refactor: adapt errors in apps

This commit is contained in:
Ali BARIN
2022-11-06 18:55:38 +01:00
committed by Faruk AYDIN
parent 5df7867b6d
commit ad46b3eea3
6 changed files with 80 additions and 104 deletions

View File

@@ -1,4 +1,4 @@
import { IJSONObject, IField, IGlobalVariable } from '@automatisch/types'; import { IField, IGlobalVariable } from '@automatisch/types';
import { URLSearchParams } from 'url'; import { URLSearchParams } from 'url';
export default async function createAuthData($: IGlobalVariable) { export default async function createAuthData($: IGlobalVariable) {
@@ -20,12 +20,8 @@ export default async function createAuthData($: IGlobalVariable) {
accessSecret: responseData.oauth_token_secret, accessSecret: responseData.oauth_token_secret,
}); });
} catch (error) { } catch (error) {
const errorMessages = error.response.data.errors
.map((error: IJSONObject) => error.message)
.join(' ');
throw new Error( throw new Error(
`Error occured while verifying credentials: ${errorMessages}` `Error occured while verifying credentials: ${error.message}`
); );
} }
} }

View File

@@ -2,25 +2,21 @@ import { IGlobalVariable } from '@automatisch/types';
import { URLSearchParams } from 'url'; import { URLSearchParams } from 'url';
const verifyCredentials = async ($: IGlobalVariable) => { const verifyCredentials = async ($: IGlobalVariable) => {
try { const response = await $.http.post(
const response = await $.http.post( `/oauth/access_token?oauth_verifier=${$.auth.data.oauthVerifier}&oauth_token=${$.auth.data.accessToken}`,
`/oauth/access_token?oauth_verifier=${$.auth.data.oauthVerifier}&oauth_token=${$.auth.data.accessToken}`, null
null );
);
const responseData = Object.fromEntries(new URLSearchParams(response.data)); const responseData = Object.fromEntries(new URLSearchParams(response.data));
await $.auth.set({ await $.auth.set({
consumerKey: $.auth.data.consumerKey, consumerKey: $.auth.data.consumerKey,
consumerSecret: $.auth.data.consumerSecret, consumerSecret: $.auth.data.consumerSecret,
accessToken: responseData.oauth_token, accessToken: responseData.oauth_token,
accessSecret: responseData.oauth_token_secret, accessSecret: responseData.oauth_token_secret,
userId: responseData.user_nsid, userId: responseData.user_nsid,
screenName: responseData.fullname, screenName: responseData.fullname,
}); });
} catch (error) {
throw new Error(error.response.data);
}
}; };
export default verifyCredentials; export default verifyCredentials;

View File

@@ -2,39 +2,35 @@ import { IGlobalVariable } from '@automatisch/types';
import getCurrentUser from '../common/get-current-user'; import getCurrentUser from '../common/get-current-user';
const verifyCredentials = async ($: IGlobalVariable) => { const verifyCredentials = async ($: IGlobalVariable) => {
try { const response = await $.http.post(
const response = await $.http.post( `${$.app.baseUrl}/login/oauth/access_token`,
`${$.app.baseUrl}/login/oauth/access_token`, {
{ client_id: $.auth.data.consumerKey,
client_id: $.auth.data.consumerKey, client_secret: $.auth.data.consumerSecret,
client_secret: $.auth.data.consumerSecret, code: $.auth.data.oauthVerifier,
code: $.auth.data.oauthVerifier, },
{
headers: {
Accept: 'application/json',
}, },
{ }
headers: { );
Accept: 'application/json',
},
}
);
const data = response.data; const data = response.data;
$.auth.data.accessToken = data.access_token; $.auth.data.accessToken = data.access_token;
const currentUser = await getCurrentUser($); const currentUser = await getCurrentUser($);
await $.auth.set({ await $.auth.set({
consumerKey: $.auth.data.consumerKey, consumerKey: $.auth.data.consumerKey,
consumerSecret: $.auth.data.consumerSecret, consumerSecret: $.auth.data.consumerSecret,
accessToken: data.access_token, accessToken: data.access_token,
scope: data.scope, scope: data.scope,
tokenType: data.token_type, tokenType: data.token_type,
userId: currentUser.id, userId: currentUser.id,
screenName: currentUser.login, screenName: currentUser.login,
}); });
} catch (error) {
throw new Error(error.response.data);
}
}; };
export default verifyCredentials; export default verifyCredentials;

View File

@@ -3,39 +3,35 @@ import getCurrentUser from '../common/get-current-user';
import qs from 'qs'; import qs from 'qs';
const verifyCredentials = async ($: IGlobalVariable) => { const verifyCredentials = async ($: IGlobalVariable) => {
try { const oauthRedirectUrlField = $.app.auth.fields.find(
const oauthRedirectUrlField = $.app.auth.fields.find( (field) => field.key == 'oAuthRedirectUrl'
(field) => field.key == 'oAuthRedirectUrl' );
); const redirectUri = oauthRedirectUrlField.value;
const redirectUri = oauthRedirectUrlField.value; const searchParams = qs.stringify({
const searchParams = qs.stringify({ code: $.auth.data.code,
code: $.auth.data.code, grant_type: 'authorization_code',
grant_type: 'authorization_code', client_id: $.auth.data.consumerKey as string,
client_id: $.auth.data.consumerKey as string, client_secret: $.auth.data.consumerSecret as string,
client_secret: $.auth.data.consumerSecret as string, redirect_uri: redirectUri,
redirect_uri: redirectUri, });
}); const { data } = await $.http.post(
const { data } = await $.http.post( `${$.auth.data.oauth2Url}/token?${searchParams}`
`${$.auth.data.oauth2Url}/token?${searchParams}` );
);
await $.auth.set({ await $.auth.set({
accessToken: data.access_token, accessToken: data.access_token,
tokenType: data.token_type, tokenType: data.token_type,
instanceUrl: data.instance_url, instanceUrl: data.instance_url,
signature: data.signature, signature: data.signature,
userId: data.id, userId: data.id,
screenName: data.instance_url, screenName: data.instance_url,
}); });
const currentUser = await getCurrentUser($); const currentUser = await getCurrentUser($);
await $.auth.set({ await $.auth.set({
screenName: `${currentUser.displayName} - ${data.instance_url}`, screenName: `${currentUser.displayName} - ${data.instance_url}`,
}); });
} catch (error) {
throw new Error(error.response.data);
}
}; };
export default verifyCredentials; export default verifyCredentials;

View File

@@ -1,15 +1,11 @@
import { IGlobalVariable } from '@automatisch/types'; import { IGlobalVariable } from '@automatisch/types';
const verifyCredentials = async ($: IGlobalVariable) => { const verifyCredentials = async ($: IGlobalVariable) => {
try { await $.http.get('/2010-04-01/Accounts.json?PageSize=1');
await $.http.get('/2010-04-01/Accounts.json?PageSize=1');
await $.auth.set({ await $.auth.set({
screenName: $.auth.data.accountSid, screenName: $.auth.data.accountSid,
}); });
} catch (error) {
throw new Error(JSON.stringify(error.response.data));
}
}; };
export default verifyCredentials; export default verifyCredentials;

View File

@@ -2,23 +2,19 @@ import { IGlobalVariable } from '@automatisch/types';
import { URLSearchParams } from 'url'; import { URLSearchParams } from 'url';
const verifyCredentials = async ($: IGlobalVariable) => { const verifyCredentials = async ($: IGlobalVariable) => {
try { const response = await $.http.post(
const response = await $.http.post( `/oauth/access_token?oauth_verifier=${$.auth.data.oauthVerifier}&oauth_token=${$.auth.data.accessToken}`,
`/oauth/access_token?oauth_verifier=${$.auth.data.oauthVerifier}&oauth_token=${$.auth.data.accessToken}`, null
null );
);
const responseData = Object.fromEntries(new URLSearchParams(response.data)); const responseData = Object.fromEntries(new URLSearchParams(response.data));
await $.auth.set({ await $.auth.set({
accessToken: responseData.oauth_token, accessToken: responseData.oauth_token,
accessSecret: responseData.oauth_token_secret, accessSecret: responseData.oauth_token_secret,
userId: responseData.user_id, userId: responseData.user_id,
screenName: responseData.screen_name, screenName: responseData.screen_name,
}); });
} catch (error) {
throw new Error(error.response.data);
}
}; };
export default verifyCredentials; export default verifyCredentials;