feat: add refresh token capability

This commit is contained in:
Ali BARIN
2022-11-17 23:14:48 +01:00
parent e3e8f570f8
commit 901605fd9c
3 changed files with 23 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
import generateAuthUrl from './generate-auth-url'; import generateAuthUrl from './generate-auth-url';
import verifyCredentials from './verify-credentials'; import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified'; import isStillVerified from './is-still-verified';
import refreshAccessToken from './refresh-access-token';
export default { export default {
fields: [ fields: [
@@ -61,6 +62,7 @@ export default {
}, },
], ],
refreshAccessToken,
generateAuthUrl, generateAuthUrl,
verifyCredentials, verifyCredentials,
isStillVerified, isStillVerified,

View File

@@ -39,7 +39,25 @@ export default function createHttpClient({
instance.interceptors.response.use( instance.interceptors.response.use(
(response) => response, (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); throw new HttpError(error);
} }
); );

View File

@@ -179,8 +179,9 @@ export interface IDynamicData {
export interface IAuth { export interface IAuth {
generateAuthUrl?($: IGlobalVariable): Promise<void>; generateAuthUrl?($: IGlobalVariable): Promise<void>;
verifyCredentials($: IGlobalVariable): Promise<any>; verifyCredentials($: IGlobalVariable): Promise<void>;
isStillVerified($: IGlobalVariable): Promise<boolean>; isStillVerified($: IGlobalVariable): Promise<boolean>;
refreshAccessToken?($: IGlobalVariable): Promise<void>;
fields: IField[]; fields: IField[];
authenticationSteps?: IAuthenticationStep[]; authenticationSteps?: IAuthenticationStep[];
reconnectionSteps?: IAuthenticationStep[]; reconnectionSteps?: IAuthenticationStep[];