axios: Interceptors - how to prevent intercepted messages from resolving as error
I’m trying to make an interceptor for 401 responses that result from expired token. Upon interception I want to login and retry the requests with the new token. My problem is that login is also done asynchronously, so by the time the retry happens, the original promises reject. Is there a way around that? Here’s my code:
axios.interceptors.response.use(undefined, err => {
if (err.status === 401 && err.config && !err.config.__isRetryRequest) {
refreshLogin(getRefreshToken(),
success => {
setTokens(success.access_token, success.refresh_token)
err.config.__isRetryRequest = true
err.config.headers.Authorization = 'Bearer ' + getAccessToken()
axios(err.config)
},
error => { console.log('Refresh login error: ', error) }
)
}
})
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 33 (2 by maintainers)
You could do something like this:
Sorry for the delay, I am just seeing this issue.
You need to make a return statement from your interceptor which will keep the Promise alive. More or less change your
refreshLogin
to return a Promise and then return that from your interceptor.Or if you can’t refactor
refreshLogin
you can wrap it in a Promise.The console.log returns undefined.
Demo project Gist link
Is this issue resolved? I am using 0.15.2 and running into this weird problem
That was with 0.12.0. Now that I updated to 0.13.1,
err
has become a string with call stack.I solved it, the 401 doesn’t have
cors
. If any one using Laravel with Laravel Cors Package then here is the solution and you will haveerror.response
availablehttps://github.com/barryvdh/laravel-cors/issues/89
I used @geocine example from Oct, 5th 2016. I am using
Vue.js 2.0
my solution was to watch the token for changes in theVuex
if it changed then to update anything that changed. I.E. if someone wanted to like a post but the JWT was expired, it intercepts the request and runs it again once the JWT has been refreshed.This is also an issue for me - v.0.15.3 - the error is just a callstack error string
I am pretty sure it should be
error.response.status
you could check out the error object if it has those properties. I don’t know why it doesn’t work on your end if you only have that isolated sample code similar to what you have posted above.I had the problem, that
error
wasundefined
in a response interceptor and it was caused by not resolving the promise forconfig
in a token refresh request interceptor, if the token refresh failed.Faulty code in request interceptor:
Fixed code:
Yes I am using the axios with Vuejs App, This is how I did
In the response interceptor rejection function, the
error.response.status
is returned correctly in the latest version (0.15.3).Here, error.response return undefined in 0.14.0 version.
Based on this sample: https://github.com/axios/axios/issues/450#issuecomment-247446276
I have used this version for refreshing token - as is described on post above. This interceptor is created for prevent refresh token repeatly if it is created more request and you probable want to call this operation only once.
This is typescript version but I think it is very similiar to js version.
I was able to adapt @rlambertsen example. Worked fine.
is this solved ? im using version 0.15.3 but
error.response
isundefined
. i try to handle 401 error. btw im using “Custom instance”