我正在使用 JWT
策略学习 passport.js,我想创建一个系统来注销用户。
我想这样处理:
- 当用户注销时,他的 token 将存储在我的数据库中名为
InvalidTokens
的表中 - 每次用户发出 protected 请求时,我都想检查他的 token 是否在表
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);
}
}
));
最佳答案
According to the doc您可以通过将 passReqToCallback
设置为 true
request
对象传递给回调
尚未测试过,但应该是正确的方向
var opts = {
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: 'secret',
passReqToCallback: true // <----- Add this
}
passport.use('jwt', new JWTStrategy(
opts,
async (req, jwtPayload, done) => {
const rawJWTToken = req['Authorization'].split(' ')[1]
...
}
));
关于javascript - 从回调中访问 jwtFromRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55694115/