I'm working on a larger web-application, which makes several calls to the server from different places. We are using a token which has a lifetime of 15min. Which means it uses the refreshtoken to generate a new token and a new refreshtoken when it gets expired. We are using an interceptor of axios to see that a token got expired. Based on that we are executing a call to generate a new one.
The issue right now is, that all API calls that are made during the time when we make a request to get a new token, will fail!
So my question I have right now is if it is possible to prevent sending requests before a specific call is completely executed (using axios)?
export const runRefreshToken = (errorCallback: () => void): void => {
Axios.interceptors.response.use(
(response: AxiosResponse<any>): AxiosResponse<any> => {
return response;
}, (error: any): any => {
const original = error.config;
const loginVerification = !(original.url as string).startsWith("api/login");
if (error.response && error.response.status === 401 && error.config && loginVerification && !error.config.__retry) {
return new Promise((resolve): void => {
original.__retry = true;
const response = fetch("api/login/refresh-token", { method: 'POST' })
.then((): AxiosPromise<any> => {
return Axios(original);
})
.catch((): void => errorCallback())
resolve(response);
});
} else if (error.config.__retry && original.url !== "api/login/verify-authentication") {
history.push("/login");
}
return Promise.reject(error);
}
);
}
Thanks in advance