Correct way of checking if a jwt token has expired in passport jwt

1.4k views Asked by At

I have a created a middleware to check if the jwt token has expired.

For some reason the TokenHasExpiredError is not intercepted as an error in passport.authenticate.

Here's my middleware

import { Request, Response, NextFunction } from 'express';
import passport from 'passport';

import { ApiError } from '../classes/error';

const authJwt = (req: Request, res: Response, next: NextFunction) => {
  passport.authenticate('jwt', function (err, user, info) {
    // TokenExpiredError is not considered as an error?
    if (err) return next(err);
    if (!user) {
      if (info.name === 'TokenExpiredError') {
        return next(ApiError.jwtTokenExpired('Jwt Token Expired'));
      }
      return next(ApiError.unauthenticated('User is not authenticated'));
    }
    req.user = user;
    next();
  })(req, res, next);
};

export default authJwt;

I have a couple of questions:

  1. From the code above, why is TokenExpiredError or any other error not intercepted as an error.
  2. What is the info parameter in the callback? I can't find it in the documentation.
0

There are 0 answers