Retrying request after refreshing JWT access token in response interceptor
See original GitHub issueI understand that interceptors are a good way to um, intercept, when my server responds with 401 because the JWT access token has expired. So far, I have something like:
const api = new Frisbee(...);
const refreshAccessToken = async () => {
const refreshToken = await SecureStore.getItemAsync('refreshToken');
api.jwt(refreshToken);
const res = await post('/users/refresh/');
api.jwt(res.body.access_token);
};
const accessTokenRefresher = {
async response(response) {
if (response.status === 401 && response.body.msg === 'Token has expired') {
await refreshAccessToken();
}
return response;
},
};
api.interceptor.register(accessTokenRefresher);
This is a good start. My Expo app calls /users/refresh/
on 401, and updates the access token as expected. But then I need to call the original URL again (the one that returned the 401 in the first place). What’s the recommended pattern for this? (I can’t access request
from response
.) Do I need to separately maintain some global state for the last attempted request, and exclude /users/refresh/
from that? It feels messy, hope there’s a nicer way to do this.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Angular 4 Interceptor retry requests after token refresh
I am successfully caching the failed requests and can refresh the token but I cannot figure out how to resend the requests that...
Read more >Retrying request after refreshing JWT access token in ... - GitHub
I understand that interceptors are a good way to um, intercept, when my server responds with 401 because the JWT access token has...
Read more >Using Axios interceptors for refreshing your API token.
After the token expires, you'll need to request a new token using the refresh token. Then, you need to use the freshly retrieved...
Read more >Angular 12 Refresh Token with Interceptor and JWT example
– When the Access Token is expired, Angular automatically sends Refresh Token request, receives new Access Token and uses it for new request....
Read more >Handle Refresh Tokens with Axios - JavaScript in Plain English
Handle expired token with Axios · The first step is to get the config from the request input it in config. · After...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@rommyarb yes this works great for React.
Closing in favor of #92