Koa2 + passport + passport-jwt 做 auth 验证报错。

大板栗
  • 400
const passport = require('passport');
const { Strategy, ExtractJwt } = require('passport-jwt');

const jwt = new Strategy({
    jwtFromRequest: ExtractJwt.fromAuthHeader(),
    secretOrKey: `secret`,
}, async (payload, done) => {
    console.log(payload, done);
});

passport.use(jwt);

exports.authJWT = passport.authenticate('jwt', {
    session: false,
});
/Users/lbc/Documents/gitstore/echo/node_modules/passport-jwt/lib/extract_jwt.js:55
        if (request.headers[AUTH_HEADER]) {
                   ^

TypeError: Cannot read property 'headers' of undefined
    at JwtStrategy._jwtFromRequest (/Users/lbc/Documents/gitstore/echo/node_modules/passport-jwt/lib/extract_jwt.js:55:20)
    at JwtStrategy.authenticate (/Users/lbc/Documents/gitstore/echo/node_modules/passport-jwt/lib/strategy.js:83:22)
    at attempt (/Users/lbc/Documents/gitstore/echo/node_modules/passport/lib/middleware/authenticate.js:349:16)
    at authenticate (/Users/lbc/Documents/gitstore/echo/node_modules/passport/lib/middleware/authenticate.js:350:7)
    at Object.<anonymous> (/Users/lbc/Documents/gitstore/echo/route/user.js:7:16)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/lbc/Documents/gitstore/echo/route/index.js:3:14)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
回复
阅读 4.3k
1 个回答
宣传栏