question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Retrying request after refreshing JWT access token in response interceptor

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
niftylettucecommented, Mar 18, 2019

@rommyarb yes this works great for React.

0reactions
niftylettucecommented, Jun 3, 2019

Closing in favor of #92

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found