axios-auth-refresh icon indicating copy to clipboard operation
axios-auth-refresh copied to clipboard

Interceptor does not work with multiple interceptors when attempting to pause

Open tarang9211 opened this issue 3 years ago • 5 comments

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.

image

tarang9211 avatar May 06 '21 08:05 tarang9211

I'm having the same problem. Did anybody ever figure this out?

alb3rtuk avatar Aug 23 '21 16:08 alb3rtuk

Any updates?

IronTony avatar Oct 22 '21 18:10 IronTony

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 !

Frumba avatar Oct 29 '21 09:10 Frumba

I remove pauseInstanceWhileRefreshing: true and added in the refreshToken method this:

axios.post(url,
{
   refreshToken,
},
{
    header: {
        ............,
     },
        skipAuthRefresh: true,
},
);

IronTony avatar Oct 29 '21 10:10 IronTony

lol, I wrote my own Interceptor middleware and I am struggling with exactly same issue now 😅

landsman avatar Dec 30 '21 19:12 landsman