Passport js本地策略自定义回调,显示用户为false,信息为“缺少凭据” [英] Passport js local strategy custom callback showing user as false and info as “Missing credentials”

查看:579
本文介绍了Passport js本地策略自定义回调,显示用户为false,信息为“缺少凭据”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node.js,angularjs,express和护照。我已经尝试了我可以想到的所有选项,但仍然没有解决方案。找不到其他帖子的解决方案。

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆