【发布时间】:2019-09-05 17:26:34
【问题描述】:
我正在学习 passport.js 和 JWT 策略,我想创建一个系统来注销用户。
我想这样处理:
- 当用户注销时,他的令牌存储在我的数据库中,名为
InvalidTokens的表中 - 每次用户发出受保护的请求时,我都想检查他的令牌是否在表中
InvalidTokens中
问题是我不知道如何访问下面代码中的字段jwtFromRequest:
// passport.js
// File where I store my authentication strategies
// ...
/**
* Use JWT strategy for all the other requests that need authentication on the
* server
*/
var opts = {
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: 'secret',
}
passport.use('jwt', new JWTStrategy(
opts,
async (jwtPayload, done) => {
try {
const token = await TokenInvalide.findOne({
where: {
Token: '<token_value_I_can\'t_reach>',
}
})
if (token !== null)
return done(null, false);
return done(null, jwtPayload.idUtilisateur);
} catch (e) {
console.log(e);
return done(null, false);
}
}
));
【问题讨论】:
标签: javascript node.js authentication passport.js passport-jwt