Users get logged out suddenly when their JWT access token expires. You want to refresh the token automatically without requiring re-login, but struggling with the implementation.

Implement a refresh token strategy: issue a short-lived access token (15min) and a long-lived refresh token (7 days). Intercept 401 responses and use the refresh token to get a new access token.

Step-by-Step Guide

1

Backend: Generate two tokens on login: accessToken (15min) and refreshToken (7 days)

2

Store refreshToken in httpOnly cookie (more secure than localStorage)

3

Frontend: On 401 response, call /refresh endpoint with refreshToken

4

If refresh succeeds, retry the original request with new accessToken

5

If refresh fails (expired/invalid), redirect to login

6

Use axios interceptors for automatic retry logic

7

Backend: Maintain a refreshToken whitelist in Redis for revocation support

8

Implement token rotation: issue new refresh token on each refresh

Found an issue with this solution?

Related Topics

jwt security best practicesrefresh token rotationtoken blacklist implementation