axios-auth-refresh
axios-auth-refresh copied to clipboard
Interceptor does not work with multiple interceptors when attempting to pause
I am using this library to pause requests that fail with a 401 and attempt to refresh a token when these requests fail. My use also involves attaching request
and response
interceptors.
Here' the code I'm working with -
const axiosInstance = axios.create({ baseURL: HOST });
const createAuthRefreshLogic = failedRequest => axiosInstance.post('/auth/public/login', {}, {
skipAuthRefresh: true,
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${btoa(`${CLIENT_ID}:${CLIENT_SECRET}`)}`
},
}).then((result) => {
console.log('createAuthRefreshLogic');
failedRequest.response.config.headers.Authorization = `Bearer ${result.data.token}`;
return Promise.resolve();
});
axiosInstance.interceptors.request.use(
(config) => {
const latestToken = AuthService.getAuthServiceInstance().getAuthToken();
console.log('request interceptor');
if (config.url === '/auth/public/login') {
// eslint-disable-next-line no-param-reassign
config.headers.Authorization = `Basic ${btoa(`${CLIENT_ID}:${CLIENT_SECRET}`)}`;
} else {
// eslint-disable-next-line no-param-reassign
config.headers.Authorization = `Bearer ${latestToken}`;
}
return config;
}
);
axiosInstance.interceptors.response.use(
(response) => {
console.log('response interceptor');
GlobalService.getGlobalServiceInstance().setLastUpdatedAt(Date.now());
return response;
},
// Attempting to handle the errors here does not seem to work
(error) => throw new ApiError(error) // This causes the the createAuthRefreshLogic to not be invoked
);
From the approach above, the requests don't seem to get paused for some reason? Is there an issue with how I'm approaching it.
~If I uncomment the request and response interceptors, the createAuthRefreshLogic
interceptor does not get invoked. Anything I'm doing wrong?~
Update
I was able to get a part of the request and response interceptors to work with the createAuthRefreshLogic function.
However, trying to handle the errors within the response interceptor causes createAuthRefreshLogic
to not be invoked, but I still see two 401 requests while neither seem to be paused.
Here's a screen shot of the network requests failing. The 401s don't seem to be getting paused.

I'm having the same problem. Did anybody ever figure this out?
Any updates?
I am having the same issue here. When having two simultaneous request with pauseInstanceWhileRefreshing: true
receiving 401 only the one who pass through the createAuthRefreshLogic interceptor is retrying. Any idea ? :D Thanks !
I remove pauseInstanceWhileRefreshing: true
and added in the refreshToken method this:
axios.post(url,
{
refreshToken,
},
{
header: {
............,
},
skipAuthRefresh: true,
},
);
lol, I wrote my own Interceptor middleware and I am struggling with exactly same issue now 😅