passport.use(
new passportJWTStrategy(config.jwt.passportJWT, (jwt_payload, next) => {
if (!jwt_payload || !jwt_payload.exp || !jwt_payload.id) {
next(new CustomError('UNAUTHORIZED', 'Token invalid.', 401), false);
} else {
const exp = jwt_payload.exp;
const currentTime = Date.now() / 1000;
if (exp < currentTime) {
next(new CustomError('UNAUTHORIZED', 'Token expired.', 401), false);
} else {
const userid = ObjectId(jwt_payload.id);
LoggedInUser.findById(userid).select('+password').exec((error, user) => {
if (error) {
next(error, false);
} else if (user) {
next(null, user);
} else {
next(new CustomError('UNAUTHORIZED', 'Associated user not found.', 401), false);
}
});
}
}
})
);