How to check if a JWT token has expired?

I added the expiresIn option to set a lifetime for the JWT tokens:

options: {
  expiresIn: 'lifetime in seconds'
}

How do you check if the access token -of a given user- has expired or not yet ?

1 Like

By default, if the access token is expired, Adonis already returns a 401 response. The request does not get to the controller, it stops at the auth middleware.

1 Like

Thank you, but for this option to work, we have to wait for the server’s response, whereas I want to take decisions upon token expiration, then send a response to the client.

Hi, a idea is create cron job that check every hour all tokens, if has expired send to client via websocket channel "oopps, you session has expired =) ".

it’s just an idea of what I understood :+1:

If you want to check if token is valid in AdonisJS:
Use auth.check this will also check if token is expired (Adonis src)

If you want to check it on frontend unpack JWT and extract exp date from there.
Or just use some library like that does it for you.

3 Likes

Great idea,absolutely agree with you