Passport js本地策略自定义回调,显示用户为false,信息为“缺少凭据” [英] Passport js local strategy custom callback showing user as false and info as “Missing credentials”
问题描述
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在控制台中显示正确,但在护照身份验证中显示为空,虚假和信息为缺少凭据。
我已经使用以下通行口
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.Compa rePassword(密码,(err,isMatch)=> {
if(err){return done(err); }
if(isMatch){
return done(null,user);
}
return done(null,false,{msg:'无效的电子邮件或密码。
});
});
}));
passport.serializeUser((user,done)=> {
// console.log(user,done);
done(null,user。 id);
});
passport.deserializeUser((id,done)=> {
User.findById(id,(err,user)=> {
done(err,user) ;
});
});
我不明白为什么问题存在。
/ p>
使用 {usernameField:'email'}
不是 {usernameField:'Email'}
因为您发送 req.body.email
不是 req.body.Email
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);
...
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?!?!
Thanks!
Use { usernameField: 'email' }
not { usernameField: 'Email' }
because you send req.body.email
not req.body.Email
这篇关于Passport js本地策略自定义回调,显示用户为false,信息为“缺少凭据”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!