feat: Add isRefreshTokenRequested condition to auth in global variable
This commit is contained in:
@@ -1,7 +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';
|
import refreshToken from './refresh-token';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
fields: [
|
fields: [
|
||||||
@@ -62,7 +62,7 @@ export default {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|
||||||
refreshAccessToken,
|
refreshToken,
|
||||||
generateAuthUrl,
|
generateAuthUrl,
|
||||||
verifyCredentials,
|
verifyCredentials,
|
||||||
isStillVerified,
|
isStillVerified,
|
||||||
|
@@ -1,20 +1,16 @@
|
|||||||
import { IGlobalVariable } from '@automatisch/types';
|
import { IGlobalVariable } from '@automatisch/types';
|
||||||
import qs from 'querystring';
|
import qs from 'querystring';
|
||||||
|
|
||||||
const refreshAccessToken = async ($: IGlobalVariable) => {
|
const refreshToken = async ($: IGlobalVariable) => {
|
||||||
const searchParams = qs.stringify({
|
const searchParams = qs.stringify({
|
||||||
grant_type: 'refresh_token',
|
grant_type: 'refresh_token',
|
||||||
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,
|
||||||
refresh_token: $.auth.data.refreshToken as string,
|
refresh_token: $.auth.data.refreshToken as string,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { data } = await $.http.post(
|
const { data } = await $.http.post(
|
||||||
`${$.auth.data.oauth2Url}/token?${searchParams}`,
|
`${$.auth.data.oauth2Url}/token?${searchParams}`
|
||||||
{
|
|
||||||
additionalProperties: {
|
|
||||||
shouldRetry: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
await $.auth.set({
|
await $.auth.set({
|
||||||
@@ -26,4 +22,4 @@ const refreshAccessToken = async ($: IGlobalVariable) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export default refreshAccessToken;
|
export default refreshToken;
|
@@ -43,17 +43,17 @@ export default function createHttpClient({
|
|||||||
const { config } = error;
|
const { config } = error;
|
||||||
const { status } = error.response;
|
const { status } = error.response;
|
||||||
|
|
||||||
if (status === 401 && $.app.auth.refreshAccessToken && config.additionalProperties?.shouldRetry !== false) {
|
if (
|
||||||
await $.app.auth.refreshAccessToken($);
|
status === 401 &&
|
||||||
|
$.app.auth.refreshToken &&
|
||||||
|
!$.app.auth.isRefreshTokenRequested
|
||||||
|
) {
|
||||||
|
$.app.auth.isRefreshTokenRequested = true;
|
||||||
|
await $.app.auth.refreshToken($);
|
||||||
|
|
||||||
// retry the request
|
// retry the previous request before the expired token error
|
||||||
const newResponse = await instance.request({
|
const newResponse = await instance.request(config);
|
||||||
...config,
|
$.app.auth.isRefreshTokenRequested = false;
|
||||||
additionalProperties: {
|
|
||||||
...(config.additionalProperties || {}),
|
|
||||||
shouldRetry: false,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return newResponse;
|
return newResponse;
|
||||||
}
|
}
|
||||||
|
3
packages/types/index.d.ts
vendored
3
packages/types/index.d.ts
vendored
@@ -181,7 +181,8 @@ export interface IAuth {
|
|||||||
generateAuthUrl?($: IGlobalVariable): Promise<void>;
|
generateAuthUrl?($: IGlobalVariable): Promise<void>;
|
||||||
verifyCredentials($: IGlobalVariable): Promise<void>;
|
verifyCredentials($: IGlobalVariable): Promise<void>;
|
||||||
isStillVerified($: IGlobalVariable): Promise<boolean>;
|
isStillVerified($: IGlobalVariable): Promise<boolean>;
|
||||||
refreshAccessToken?($: IGlobalVariable): Promise<void>;
|
refreshToken?($: IGlobalVariable): Promise<void>;
|
||||||
|
isRefreshTokenRequested: boolean;
|
||||||
fields: IField[];
|
fields: IField[];
|
||||||
authenticationSteps?: IAuthenticationStep[];
|
authenticationSteps?: IAuthenticationStep[];
|
||||||
reconnectionSteps?: IAuthenticationStep[];
|
reconnectionSteps?: IAuthenticationStep[];
|
||||||
|
Reference in New Issue
Block a user