Passport-jwt token expiration
express, jwt, node.js, passport.js
Solution
The standard for JWT is to include the expiry in the payload as "exp". If you do that, the passport-JWT module will respect it unless you explicitly tell it not to. Easier than implementing it yourself.
EDIT
Now with more code!
I typically use the npm module `jsonwebtoken` for actually creating/signing my tokens, which has an option for setting expiration using friendly time offsets in the exp element of the payload. It works like so:
const jwt = require('jsonwebtoken');
// in your login route
router.post('/login', (req, res) => {
// do whatever you do to handle authentication, then issue the token:
const token = jwt.sign(req.user, 's00perS3kritCode', { expiresIn: '30m' });
res.send({ token });
});
Your JWT Strategy can then look like what you have already, from what I see, and it will automatically respect the expiration time of 30 minutes that I set above (obviously , you can set other times).
Problem
I am using passport-jwt to generate my tokens but I noticed that the tokens never expire, is there any way to invalidate a particular token according to a rule set for me, something like: ``` 'use strict'; const passport = require('passport'); const passportJWT = require('passport-jwt'); const ExtractJwt = passportJWT.ExtractJwt; const Strategy = passportJWT.Strategy; const jwt = require('../jwt'); const cfg = jwt.authSecret(); const params = { secretOrKey: cfg.jwtSecret, jwtFromRequest: ExtractJwt.fromAuthHeader() }; module.exports = () => { const strategy = new Strategy(params, (payload, done) => { //TODO: Create a custom validate strategy done(null, payload); }); passport.use(strategy); return { initialize: function() { return passport.initialize(); }, authenticate: function() { //TODO: Check if the token is in the expired list return passport.authenticate('jwt', cfg.jwtSession); } }; }; ``` or some strategy to invalidate some tokens