I am developing MEAN stack application. for session authentication I used express-jwt.
My code is working very well with express-jwt token but when I log out then I want to remove jwt token / add token in black list.
So when I apply express-jwt-blacklist with jwt token it throws an error.Error: JWT missing tokenId claimsub
My working code
=> In middleware / config file
const expressJWT = require("express-jwt");
CONFIG.JWTTOKENALLOWACCESS = expressJWT({
secret: CONFIG.JWTTOKENKEY,
userProperty: 'payload'
});
=> In routing file
router.route("/get-post-list").get(CONFIG.JWTTOKENALLOWACCESS, PostCtrl.getPostList);
My not working code (after apply express-jwt-blacklist)
=> In middleware / config file
const expressJWT = require("express-jwt");
const blacklist = require('express-jwt-blacklist');
CONFIG.JWTTOKENALLOWACCESS = expressJWT({
secret: CONFIG.JWTTOKENKEY,
userProperty: 'payload',
isRevoked: blacklist.isRevoked
});
=> In routing file
router.route("/get-post-list").get(CONFIG.JWTTOKENALLOWACCESS, PostCtrl.getPostList);
Error throws
error: "JWT missing tokenId claimsub"
Please help me to resolve this.
Issue resolved by adding tokenId in blacklist.configure like as bellow
=> In middleware / config file
const expressJWT = require("express-jwt");
const blacklist = require('express-jwt-blacklist');
blacklist.configure({
tokenId: 'jti',
// strict: true,
// store: {
// type: 'memcached',
// host: 'localhost',
// port: 3001,
// keyPrefix: 'mywebapp:',
// options: {
// timeout: 1000
// }
// }
});
CONFIG.JWTTOKENALLOWACCESS = expressJWT({
secret: CONFIG.JWTTOKENKEY,
userProperty: 'payload',
isRevoked: blacklist.isRevoked
});
=> In routing file
router.route("/get-post-list").get(CONFIG.JWTTOKENALLOWACCESS, PostCtrl.getPostList);
Make sure you must have set jti params/field at jwt login time like as bellow
const randToken = require('rand-token');
const jwt = require("jsonwebtoken");
...
...
...
user.myToken = jwt.sign({
jti : user._id + "_" + randToken.generator({ chars: '0-9' }).generate(6);
first_name : user.first_name,
last_name : user.last_name,
...
...
...
}, jwt token key, {
expiresIn: '7d' //7 days
});