refactor: Use functional design for the authentication of apps

This commit is contained in:
Faruk AYDIN
2022-10-03 21:14:54 +03:00
parent 0abab06124
commit 4d5be952ce
17 changed files with 523 additions and 22 deletions

View File

@@ -0,0 +1,43 @@
import { IJSONObject, IField } from '@automatisch/types';
import oauthClient from '../common/oauth-client';
import { URLSearchParams } from 'url';
export default async function createAuthData($: any) {
try {
const oauthRedirectUrlField = $.app.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const callbackUrl = oauthRedirectUrlField.value;
const requestData = {
url: `${$.app.baseUrl}/oauth/request_token`,
method: 'POST',
data: { oauth_callback: callbackUrl },
};
const authHeader = oauthClient($).toHeader(
oauthClient($).authorize(requestData)
);
const response = await $.http.post(`/oauth/request_token`, null, {
headers: { ...authHeader },
});
const responseData = Object.fromEntries(new URLSearchParams(response.data));
await $.auth.set({
url: `${$.app.baseUrl}/oauth/authorize?oauth_token=${responseData.oauth_token}`,
accessToken: responseData.oauth_token,
accessSecret: responseData.oauth_token_secret,
});
} catch (error) {
const errorMessages = error.response.data.errors
.map((error: IJSONObject) => error.message)
.join(' ');
throw new Error(
`Error occured while verifying credentials: ${errorMessages}`
);
}
}