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';
export default async function createAuthData($: IGlobalVariable) {
@@ -20,12 +20,8 @@ export default async function createAuthData($: IGlobalVariable) {
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}`
`Error occured while verifying credentials: ${error.message}`
);
}
}

View File

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

View File

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

View File

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

View File

@@ -1,15 +1,11 @@
import { IGlobalVariable } from '@automatisch/types';
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({
screenName: $.auth.data.accountSid,
});
} catch (error) {
throw new Error(JSON.stringify(error.response.data));
}
await $.auth.set({
screenName: $.auth.data.accountSid,
});
};
export default verifyCredentials;

View File

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