1

我正在学习带策略的passport.jsJWT ,我想创建一个系统来注销用户。

我想这样处理:

  1. 当用户注销时,他的令牌存储在我的数据库中,在一个名为InvalidTokens
  2. 每次用户发出受保护的请求时,我都想检查他的令牌是否在表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);
    }
  }
));
4

1 回答 1

2

根据文档,您可以通过将对象设置为 true来将对象传递request给回调passReqToCallback

尚未对此进行测试,但应该是正确的方向

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]

    ...
  }
));
于 2019-04-15T18:14:08.113 回答