Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - Express.js JWT, how to check token expired or not?

how to check whether my token is expired or not?

var token = jwt.sign(user,app.get('superSecret'),{
    expiresIn : 2
});
like image 739
Karthik Avatar asked Nov 27 '22 00:11

Karthik


1 Answers

I assume you are using the jsonwebtoken package that is documented here

If that is the case, have a look at the jwt.verify method:

jwt.verify(token, 'shhhhh', function(err, decoded) {
  if (err) {
    /*
      err = {
        name: 'TokenExpiredError',
        message: 'jwt expired',
        expiredAt: 1408621000
      }
    */
  }
});

In short words: Check the error of that method. If it is the TokenExpiredError then, guess what... the token is expired.

like image 151
Andre Kreienbring Avatar answered Dec 10 '22 03:12

Andre Kreienbring