Home > Software engineering >  How can I create JWT refresh token on node js?
How can I create JWT refresh token on node js?

Time:05-08

I am using a simple JWT auth firebase. backend checks if its a valid user and gives back an access token using JWT. Now I want to implement a refresh token. How can I do it? What should be the content of the refresh token? When I sign a new access token and go to protected a page but when fresh it, it go to login page again. what should I do to also sign a refresh token? please help me anyone.

CodePudding user response:

when you are generating JWT auth token generate refresh token with 1d or with no expiry time according to you requirement. After this send JWT and JWT-REFRESH token in the response of login API, after this make an API in your backend which accepts the refresh token from header or from body and in response generate a JWT token, in case of bad refresh token return 401 status code.

At client side if you are using axios, you can use axios-interceptors as a middle to detect if 401 is coming from any API, then in that case hit the refresh JWT token API to generate new auth token.

If refresh token API gives again 401 response then handle it as REFRESH token is also expire and redirect the user into login page.

  • Related