I've configured the token like this:
jwt.sign( { user: pick(user, ['_id', 'username']) }, secret, { expiresIn: '2m' } );
But when I want to check if the token was expired, this code doesn't work:
function isAuthenticated() { const token = localStorage.getItem('token'); const refreshToken = localStorage.getItem('refreshToken'); try { decode(token); const { exp } = decode(refreshToken); if (exp < (new Date().getTime() + 1) / 1000) { return false; } } catch (err) { return false; } return true; }
The problem is this part:
if (exp < (new Date().getTime() + 1) / 1000) { return false; }
new Date().getTime() + 1) / 1000 = 1531335468.113
exp = 1531334595
Because I don't know what format of time JWT uses...
How can I resolve this?
verify with token and secret to verify the JWT token string against the secret string. It'll also check if token has expired.
Test Refresh Token with Spring Boot RestTemplate We will be modifying the code to test the refresh token scenario. Modify the TestController class. If we get the Expired JWT Exception, we will be creating a new refresh JWT and using it to get the data. Run the application to test refreshtoken url.
In short, you need to use REFRESH_TOKEN when ACCESS_TOKEN expires to get a new ACCESS_TOKEN. JWT has two kind of tokens: ACCESS_TOKEN and REFRESH_TOKEN.
The JWT access token is only valid for a finite period of time. Using an expired JWT will cause operations to fail.
This is the answer:
if (Date.now() >= exp * 1000) { return false; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With