From 901605fd9c4449d0c153bd02ad9068b8a90f83a2 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Thu, 17 Nov 2022 23:14:48 +0100 Subject: [PATCH] feat: add refresh token capability --- .../backend/src/apps/salesforce/auth/index.ts | 2 ++ .../backend/src/helpers/http-client/index.ts | 20 ++++++++++++++++++- packages/types/index.d.ts | 3 ++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/apps/salesforce/auth/index.ts b/packages/backend/src/apps/salesforce/auth/index.ts index 04b31160..8589bc35 100644 --- a/packages/backend/src/apps/salesforce/auth/index.ts +++ b/packages/backend/src/apps/salesforce/auth/index.ts @@ -1,6 +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'; export default { fields: [ @@ -61,6 +62,7 @@ export default { }, ], + refreshAccessToken, generateAuthUrl, verifyCredentials, isStillVerified, diff --git a/packages/backend/src/helpers/http-client/index.ts b/packages/backend/src/helpers/http-client/index.ts index 893996d9..f53f2699 100644 --- a/packages/backend/src/helpers/http-client/index.ts +++ b/packages/backend/src/helpers/http-client/index.ts @@ -39,7 +39,25 @@ export default function createHttpClient({ instance.interceptors.response.use( (response) => response, - (error) => { + async (error) => { + const { config } = error; + const { status } = error.response; + + if (status === 401 && $.app.auth.refreshAccessToken && config.additionalProperties.shouldRetry !== false) { + await $.app.auth.refreshAccessToken($); + + // retry the request + const newResponse = await instance.request({ + ...config, + additionalProperties: { + ...(config.additionalProperties || {}), + shouldRetry: false, + } + }); + + return newResponse; + } + throw new HttpError(error); } ); diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index 1edc8889..8e3a39a3 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -179,8 +179,9 @@ export interface IDynamicData { export interface IAuth { generateAuthUrl?($: IGlobalVariable): Promise; - verifyCredentials($: IGlobalVariable): Promise; + verifyCredentials($: IGlobalVariable): Promise; isStillVerified($: IGlobalVariable): Promise; + refreshAccessToken?($: IGlobalVariable): Promise; fields: IField[]; authenticationSteps?: IAuthenticationStep[]; reconnectionSteps?: IAuthenticationStep[];