From 6bf0e799a17f00ae3db4fd2b4303f09ea101a246 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Sun, 20 Nov 2022 22:28:31 +0100 Subject: [PATCH] feat: Add isRefreshTokenRequested condition to auth in global variable --- .../backend/src/apps/salesforce/auth/index.ts | 4 ++-- ...fresh-access-token.ts => refresh-token.ts} | 12 ++++------- .../backend/src/helpers/http-client/index.ts | 20 +++++++++---------- packages/types/index.d.ts | 3 ++- 4 files changed, 18 insertions(+), 21 deletions(-) rename packages/backend/src/apps/salesforce/auth/{refresh-access-token.ts => refresh-token.ts} (71%) diff --git a/packages/backend/src/apps/salesforce/auth/index.ts b/packages/backend/src/apps/salesforce/auth/index.ts index 8589bc35..c9d6f7a4 100644 --- a/packages/backend/src/apps/salesforce/auth/index.ts +++ b/packages/backend/src/apps/salesforce/auth/index.ts @@ -1,7 +1,7 @@ import generateAuthUrl from './generate-auth-url'; import verifyCredentials from './verify-credentials'; import isStillVerified from './is-still-verified'; -import refreshAccessToken from './refresh-access-token'; +import refreshToken from './refresh-token'; export default { fields: [ @@ -62,7 +62,7 @@ export default { }, ], - refreshAccessToken, + refreshToken, generateAuthUrl, verifyCredentials, isStillVerified, diff --git a/packages/backend/src/apps/salesforce/auth/refresh-access-token.ts b/packages/backend/src/apps/salesforce/auth/refresh-token.ts similarity index 71% rename from packages/backend/src/apps/salesforce/auth/refresh-access-token.ts rename to packages/backend/src/apps/salesforce/auth/refresh-token.ts index 84afb921..2ae82a10 100644 --- a/packages/backend/src/apps/salesforce/auth/refresh-access-token.ts +++ b/packages/backend/src/apps/salesforce/auth/refresh-token.ts @@ -1,20 +1,16 @@ import { IGlobalVariable } from '@automatisch/types'; import qs from 'querystring'; -const refreshAccessToken = async ($: IGlobalVariable) => { +const refreshToken = async ($: IGlobalVariable) => { const searchParams = qs.stringify({ grant_type: 'refresh_token', client_id: $.auth.data.consumerKey as string, client_secret: $.auth.data.consumerSecret as string, refresh_token: $.auth.data.refreshToken as string, }); + const { data } = await $.http.post( - `${$.auth.data.oauth2Url}/token?${searchParams}`, - { - additionalProperties: { - shouldRetry: false, - }, - }, + `${$.auth.data.oauth2Url}/token?${searchParams}` ); await $.auth.set({ @@ -26,4 +22,4 @@ const refreshAccessToken = async ($: IGlobalVariable) => { }); }; -export default refreshAccessToken; +export default refreshToken; diff --git a/packages/backend/src/helpers/http-client/index.ts b/packages/backend/src/helpers/http-client/index.ts index d6001bf3..79a7009c 100644 --- a/packages/backend/src/helpers/http-client/index.ts +++ b/packages/backend/src/helpers/http-client/index.ts @@ -43,17 +43,17 @@ export default function createHttpClient({ const { config } = error; const { status } = error.response; - if (status === 401 && $.app.auth.refreshAccessToken && config.additionalProperties?.shouldRetry !== false) { - await $.app.auth.refreshAccessToken($); + if ( + status === 401 && + $.app.auth.refreshToken && + !$.app.auth.isRefreshTokenRequested + ) { + $.app.auth.isRefreshTokenRequested = true; + await $.app.auth.refreshToken($); - // retry the request - const newResponse = await instance.request({ - ...config, - additionalProperties: { - ...(config.additionalProperties || {}), - shouldRetry: false, - } - }); + // retry the previous request before the expired token error + const newResponse = await instance.request(config); + $.app.auth.isRefreshTokenRequested = false; return newResponse; } diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index 8e3a39a3..458959ab 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -181,7 +181,8 @@ export interface IAuth { generateAuthUrl?($: IGlobalVariable): Promise; verifyCredentials($: IGlobalVariable): Promise; isStillVerified($: IGlobalVariable): Promise; - refreshAccessToken?($: IGlobalVariable): Promise; + refreshToken?($: IGlobalVariable): Promise; + isRefreshTokenRequested: boolean; fields: IField[]; authenticationSteps?: IAuthenticationStep[]; reconnectionSteps?: IAuthenticationStep[];