简体   繁体   中英

How can I request refresh token api before jwt token expires in React.js project

How can I request refresh token api before jwt token expires in React.js project. If my token time is 1 minute, I need to renew the token after 59 seconds. If I don't renew within 1 minute, my token becomes useless. Technologies I use react hooks, context api

The preferred / reliable option is to allow the access token to expire and handle 401s by retrying the API request with a new access token.

See this code for an example of how a reliable client calls APIs.

If interested in how this feels from a performance viewpoint, run my Online SPA , sign in, then click these options:

  • Expire Access Token
  • Reload Data

In some setups there can be more subtle ways for your UI to receive 401s, such as token signing certificate renewal and load balancing flips - so I always recommend that clients code this retry logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM