我在使用Password-JWT进行身份验证时遇到错误 [英] I'm getting an error authenticating with Password-JWT
问题描述
我在使用Password-Jwt进行身份验证时遇到了这个奇怪的错误,我对此很陌生,因此我们将不胜感激.password.js文件:
I'm getting this weird error authenticating with Password-Jwt, i'm kinda new to this so any help would be much appreciated. passport.js file :
const jwtStrategy = require('passport-jwt').Strategy;
const extractJwt = require('passport-jwt').ExtractJwt;
const User = require('../models/user');
const config = require('../config/database');
module.exports = function(passport){
let opts = {};
opts.jwtFromRequest =
extractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = config.secret;
passport.user(new jwtStrategy, opts, function(jwt_payload,
done){
User.getUserById(jwt_payload._id, function(err, user){
if(err){
return done(err, false);
}
if(user){
return done(null, user);
}else{
return done(null, false);
}
});
});
}
我遇到此错误:
this._secretOrKeyProvider = options.secretOrKeyProvider;^
this._secretOrKeyProvider = options.secretOrKeyProvider; ^
TypeError:无法读取未定义的属性'secretOrKeyProvider'在新的JwtStrategy(/home/mostfaubuntu/Desktop/desktop/MEAN_APP_PROJECT/node_modules/passport-jwt/lib/strategy.js:33:41)在module.exports(/home/mostfaubuntu/Desktop/desktop/MEAN_APP_PROJECT/config/passport.js:10:19)在对象.(/home/mostfaubuntu/Desktop/desktop/MEAN_APP_PROJECT/app.js:34:29)在Module._compile(module.js:652:30)在Object.Module._extensions..js(module.js:663:10)在Module.load(module.js:565:32)在tryModuleLoad(module.js:505:12)在Function.Module._load(module.js:497:3)在Function.Module.runMain(module.js:693:10)在启动时(bootstrap_node.js:188:16)
TypeError: Cannot read property 'secretOrKeyProvider' of undefined at new JwtStrategy (/home/mostfaubuntu/Desktop/desktop/MEAN_APP_PROJECT/node_modules/passport-jwt/lib/strategy.js:33:41) at module.exports (/home/mostfaubuntu/Desktop/desktop/MEAN_APP_PROJECT/config/passport.js:10:19) at Object. (/home/mostfaubuntu/Desktop/desktop/MEAN_APP_PROJECT/app.js:34:29) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Function.Module.runMain (module.js:693:10) at startup (bootstrap_node.js:188:16)
推荐答案
因此,您必须使用 new JwtStrategy(opts,callbackFunction)
查看文档中的示例用法.
Have a look at the sample usage from the documentation.
passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
User.findOne({id: jwt_payload.sub}, function(err, user) {
if (err) {
return done(err, false);
}
if (user) {
return done(null, user);
} else {
return done(null, false);
// or you could create a new account
}
});
}));
这篇关于我在使用Password-JWT进行身份验证时遇到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!