Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interceptor does not work with multiple interceptors when attempting to pause #156

Open
tarang9211 opened this issue May 6, 2021 · 5 comments

Comments

@tarang9211
Copy link

tarang9211 commented May 6, 2021

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

@alb3rtuk
Copy link

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

@IronTony
Copy link

Any updates?

@Frumba
Copy link

Frumba commented Oct 29, 2021

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 !

@IronTony
Copy link

IronTony commented Oct 29, 2021

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

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

@landsman
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants