Dealing With Expired JWT Token

El Setiyawan
1 min readAug 13, 2020

--

Photo by John Schnobrich on Unsplash

I usually get jwt token that expired in 24 hours, but this time I get only 5 minutes jwt life. The api gave me refresh token that will last for 7 days. So I have to think how to handle this in my react app. There is no way that user has to relogin every 5 minutes right?

So I come up with this idea : I will check the token expiration date before hit API. If the token already expired, I will refresh it first and use the new token to get the next service.

NOTE : check out how I build the reusable api helper here.

Axios Middleware

Axios has middleware to deal with before request and after get response called interceptors. So I use this request interceptor to check and refresh my token.

Once again, you have your own way to refresh your token right?

No? Okay. Here a simple example how you can refresh your token. Please adjust it to your need.

Happy coding.

--

--