-
Notifications
You must be signed in to change notification settings - Fork 91
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
refreshAuthLogic is not recalled on a second failure #123
Comments
Sorry, I had no time to reply last few weeks. What version are you using, please? can you share your implementation? Thanks. |
hi, I have a similar problem. my issue goes: both requests are called within a Promise.all we use |
ok, I installed |
here is my implemention axios.interceptors.request.use(config => {
if (config.method === 'POST' || config.method === 'PATCH' || config.method === 'PUT')
config.headers['Content-Type'] = 'application/json;charset=utf-8';
const accessToken = AuthService.getAccessToken();
if (accessToken && !config.headers.Authorization) config.headers.Authorization = 'Bearer ' + accessToken;
return config;
});
// Function that will be called to refresh authorization
const refreshAuthLogic = (failedRequest: any) =>
AuthService.refreshToken(AuthService.getRefreshToken()!)
.then(data => {
AuthService.storeTokens(data);
failedRequest.response.config.headers['Authorization'] = 'Bearer ' + data.accessToken;
return Promise.resolve();
})
.catch(() => {
AuthService.removeTokens();
history.push(routes.login);
});
createAuthRefreshInterceptor(axios, refreshAuthLogic);
my refresh token is simple POST request with no special arguments |
same problem |
Sorry guys, will look at it as I plan to release v3.1.0. Had a hard time at work at the end of last year and no time to do anything useful elsewhere. Are you 100% sure that this is not something which is caused by your token-binding logic? For example (in the code above) it might not work because of the following if statement. if (accessToken && !config.headers.Authorization) // Authorization header is there, but with old token I would actually bind the token every time, it doesn't really cost that much and there's no need for that check. In case of multiple tokens and/or requests that are not triggered with token I'm using another instance fo axios, as using just one introduces more edge-cases in my opinion. There are some tests for this logic explicitly, so it seems a bit odd to me, that there's would be an error like that and only a few people would notice it, while there's 21K downloads per week. Of course, if there's an error it needs to be fixed asap. I'm just making sure your implementations are working correctly. Thanks. |
I was having the exact same problem. After checking the source code and understanding how it works, I did a few tests. On the first test, I have a function with 3 calls to my API using an expired token and same Axios instance. This first test showed that the execution for these request happened sequentially. This first test did not produce any error.
Then, I did a second test where I called the function three times. The first call to the function was executed in parallel with the execution of the second call and the third call.
This second test gave me the same errors like @israelKusayev (with pauseInstanceWhileRefreshing in true or false) I am not an expert in JavaScript or Typescript. I think the responses are coming almost at the same time using the same Axios instance. Like using a different thread. The first response gets into the "refreshAuthLogic" and it creates the request interceptor that stalls any other request (when we have pauseInstanceWhileRefreshing in true). By the time when this happens, there are two more responses coming, and they will not execute the "refreshAuthLogic". I solved this situation adding an extra interceptor to resend those requests that where not handle by the "refreshAuthLogic" and that are not stalled by the request interceptor, to add them to the stalled requests.
Please let me know if you get same errors and if you have a better/cleaner solution. |
I had the same problem, and solution provided by @CteixeiraPW seems to work; In my code I made this change. Before
After
|
Now when I look at it... I think the only problem is that you're not binding the token with an interceptor. The Part of the docs explaining the reasoning behind this: https://github.com/Flyrell/axios-auth-refresh#request-interceptor |
Hello @Flyrell, I have in my app an interceptor to add the token.
If I remember well, I think in your code you are intercepting every response with a 401 and the first one triggers the refreshing of the token and create an interceptor to bind all the following requests, but the problem is that by the time that you create this "createRequestQueueInterceptor" more that one request have already been sent.
This means you may have two or more responses with 401, the first one gets a retry with the new token but not the second one. The first response will add the instance to the "skipInstances" list
But the "shouldInterceptError" will reject the second response with 401 because the instance is already in the "skipInstances" list.
This is why I think this error is happening. Please, let me know if any of my assumptions is wrong. I am not an expert in JS or React. |
Hi there, Cheers, |
Just bumping this. :) |
Can't get event when token request returns 401. $axios.onError doesn't work like all methods I've tried
|
This one has been around since 2020 :( @Flyrell do we have any plan to resolve this within the next release? |
This issue has existed for 4 years, the project I am working on is having the same problem and cannot solve it :) |
Assume that you are making a request which returns a 401.
refreshAuthLogic
will be called as it should and this second request is returning again a 401.This second failure is not triggering
refreshAuthLogic
again, is this intentional?Is there any way to re-trigger the callback?
My scenario goes as follows:
A web application is utilizing the UMA flow which uses RPT Tokens to access resources.
RPT tokens are cached and they may expire. On expiration a 401 is issued and I retry using a normal access token which will respond (again) with a 401 and a permission ticket which in turn will be exchanged for a new RPT token.
So the scenario is
request -> 401 -> request again -> 401 -> request again -> possibly 200
The text was updated successfully, but these errors were encountered: