问题描述
我正在使用 Node.js、angularjs、express 和护照.我已经尝试了我能想到的所有选项,但仍然没有解决方案.无法从其他帖子中找到解决此问题的方法.
I am using Node.js,angularjs,express and passport. I have tried all options I can think of, but still no solution. Couldn't find a solution from other posts for this problem.
app.post('/login', function(req, res, next) {
console.log(req.body.Email);
console.log(req.body.Password);
passport.authenticate('local', function(err, user, info) {
console.log(err,user,info);
...
在上面的 req.body 在控制台中显示正确,但在护照身份验证中,它显示 null、false 和 info 作为缺少凭据.
In above req.body is showing correct in console but in passport authenticate it is showing null,false and info as missing credentials.
我已将以下内容用于通行证
I have used the following for pass port
passport.use(new LocalStrategy({ usernameField: 'Email' }, (Email, Password, done) => {
console.log(Email, Password, done);
User.findOne({ Email: Email.toLowerCase() }, (err, user) => {
console.log(err);
if (err) { return done(err); }
if (!user) {
return done(null, false, { msg: `Email ${Email} not found.` });
}
user.ComparePassword(Password, (err, isMatch) => {
if (err) { return done(err); }
if (isMatch) {
return done(null, user);
}
return done(null, false, { msg: 'Invalid email or password.' });
});
});
}));
passport.serializeUser((user, done) => {
// console.log(user, done);
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user);
});
});
我不明白为什么会出现这个问题.
I couldn't understand why the problem exist.
有人知道我在这里遗漏了什么吗?!?!
Does anybody know what I am missing here?!?!
谢谢!
推荐答案
添加 upto kaxi answer 我已经通过在护照本地策略中添加密码字段来解决此错误,如下所示
Adding upto kaxi answer I have made to solve this error by adding password field in the passport local strategy as below
passport.use(new LocalStrategy({ usernameField: 'Email',passwordField: 'Password' }, (Email, Password, done)
代替
passport.use(new LocalStrategy({ usernameField: 'Email' }, (Email, Password, done)
这篇关于Passport js 本地策略自定义回调将用户显示为 false,将信息显示为“缺少凭据"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!