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 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,
|
||||
|
@@ -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;
|
@@ -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;
|
||||
}
|
||||
|
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>;
|
||||
verifyCredentials($: IGlobalVariable): Promise<void>;
|
||||
isStillVerified($: IGlobalVariable): Promise<boolean>;
|
||||
refreshAccessToken?($: IGlobalVariable): Promise<void>;
|
||||
refreshToken?($: IGlobalVariable): Promise<void>;
|
||||
isRefreshTokenRequested: boolean;
|
||||
fields: IField[];
|
||||
authenticationSteps?: IAuthenticationStep[];
|
||||
reconnectionSteps?: IAuthenticationStep[];
|
||||
|
Reference in New Issue
Block a user