passportjs 错误:未知的身份验证策略“本地" [英] Error with passportjs: Unknown authentication strategy "local"
问题描述
我正在开发一个 nodejs 应用程序并使用passportjs 进行身份验证.我正在使用护照的本地策略.但是当我尝试登录时,出现以下错误:
I am developing a nodejs application and using passportjs for authentication. I am using local strategy of passport. But when I try to login, I am getting following error:
Error: Unknown authentication strategy "local"
at attempt (/home/project/node_modules/passport/lib/middleware/authenticate.js:166:37)
at authenticate (/home/project/node_modules/passport/lib/middleware/authenticate.js:338:7)
at exports.authenticate (/home/project/controllers/RegistrationsController.js:87:4)
at callbacks (/home/project/node_modules/express/lib/router/index.js:164:37)
at param (/home/project/node_modules/express/lib/router/index.js:138:11)
at pass (/home/project/node_modules/express/lib/router/index.js:145:5)
at Router._dispatch (/home/project/node_modules/express/lib/router/index.js:173:5)
at Object.router (/home/project/node_modules/express/lib/router/index.js:33:10)
at next (/home/project/node_modules/express/node_modules/connect/lib/proto.js:193:15)
at /home/project/node_modules/express-flash/lib/express-flash.js:31:7
这是我的passport-config.js文件:
Here is my passport-config.js file:
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/user');
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
passport.use({usernameField: 'emailAddress'}, new LocalStrategy(function(username, password, done) {
User.findOne({ emailAddress: username }, function(err, user) {
if(err){
return done(err);
}
if (!user) {
return done(null, false, { message: 'Email ' + username + ' not found'});
}
else{
//check if password matches and pass parameters in done accordingly
}
});
}));
以下是我的 RegistrationsController.js 文件,我的身份验证 api 所在的位置,
And following is my RegistrationsController.js file, where my authenticate api resides,
var passport = require('passport');
exports.authenticate = function(req, res, next){
console.log('Login request!');
passport.authenticate('local', function(err, user, info) {
console.log('In authenticate callback!');
if (err) return next(err);
if (!user) {
req.flash('errors', { msg: info.message });
res.status(500).json({message: info.message});
}
res.json(user);
})(req, res, next);
}
自过去 2 天以来一直在查看代码,但仍无法找出错误.我已经安装了 passport
和 passport-local
模块.任何帮助将不胜感激.
Have been looking at the code since past 2 days but could not figure out the error yet. I have installed both passport
and passport-local
modules. Any help would be greatly appreciated.
推荐答案
此代码中存在语法错误.usernameField: 'emailAddress'
必须像这样在 LocalStrategy 构造函数中传递...
There is a syntax error in this code. usernameField: 'emailAddress'
Has to be passed in LocalStrategy constructor like this...
passport.use(new LocalStrategy({
usernameField: 'email'
}, yourAuthenticateFunction));
这篇关于passportjs 错误:未知的身份验证策略“本地"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!