-
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
Interceptors fail to assign new token on second failure #197
Comments
you should set up the assigning interceptor before const service = axios.create();
async function getAccessToken() {
const accessToken = await this.refreshToken();
const formattedAccessToken = 'Bearer' + accessToken;
return formattedAccessToken;
}
service.interceptors.request.use(async request => {
request.headers['Authorization'] = await getAccessToken();
return request;
});
const refreshAuthLogic = async failedRequest => {
// Here you just refresh your token
// You alter only the failed request
failedRequest.response.config.headers['Authorization'] = formattedAccessToken;
// Other requests are altered by the interceptor (as they've not been sent yet)
return Promise.resolve();
};
const refreshAuthOptions = {
statusCodes: [401, 403]
};
createAuthRefreshInterceptor(service, refreshAuthLogic, refreshAuthOptions); |
Thanks a bunch! service.interceptors.request.use(
request => {
// If there is a new access token, assign it to the headers.
// Can be deduced by comparing the existing access token to whatever you save in the localStorage / cookie / global variables / etc...
if (hasNewAccessToken) {
request.headers['Authorization'] = newAccessToken;
}
return request;
},
error => Promise.reject(error)
); |
I'm fairly certain my issue is somewhat related to #123.
Here is the scenario:
The first time the API request(s) fail, the auth refresh logic triggers and the newly generated token is sent with the repeated API request(s).
The second time the API request(s) fail, the auth refresh logic triggers and a new token is generated but the repeated API request(s) are still using the old token.
Network activity:
Network activity (Some explanation):
Code:
Any ideas on how I can consistently assign the new access token to the auth header?
The text was updated successfully, but these errors were encountered: