简体   繁体   English

护照验证始终执行失败

[英]Passport authenticate is always executing failureRedirect

I am using passport.js to authenticate users for my node.js backend for my app. 我正在使用passport.js来为我的应用程序的node.js后端认证用户。 The following code is always performing failureRedirect and I am not able to find the reason for it. 以下代码始终执行failureRedirect,但我无法找到原因。 There is no error message. 没有错误信息。

router.post('/login', passport.authenticate('local', {
                                                    failureRedirect: '/users/login',
                                                    failureFlash: 'Invalid email or password'
                                                }), function(req, res) {
console.log('Authentication Successful');
req.flash('success', 'You are logged in ');
res.redirect('/');
});

I copied this code from the passport website and this too is not working: 我从护照网站复制了此代码,这也无法正常工作:

router.post('/login', passport.authenticate('local', { successRedirect: '/',
                                                failureRedirect: '/users/login' }));

The following code is not even starting: 以下代码甚至还没有开始:

passport.use(new localStrategy({
                            email: 'email',
                            password: 'password'
                            }, function(email, password, done) {
User.getUserByEmail(email, function(err, user) {
    if (err) throw err;
    if (!user) {
        console.log('Unknown User');
        return done(null, false, {
            message: 'Unknown User'
        });
    }

    User.comparePassword(password, user.password, function(err, isMatch) {
        if (err) throw err;
        if (isMatch) {
            return done(null, user);
        } else {
            console.log('Invalid Password');
            return done(null, false, {
                message: 'Invalid Password'
            });
        }
    });
});
}));

Rest of the relevant code: 其余相关代码:

passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
User.getUserById(id, function(err, user) {
    done(err, user);
});
});


module.exports.getUserByEmail = function(email, callback){
var query = {email: email}; 
  User.findOne(query, function(err, user) {
    callback(err, user);
  }); 
}

module.exports.getUserById = function(id, callback){
  User.findById(id, function(err, user) {
    callback(err, user);
  }); 
}

module.exports.comparePassword = function(userPassword, hash, callback){
  console.log("pwd: " + userPassword + " hash: " + hash);
  bcrypt.compare(userPassword, hash, function(err, isMatch) {
    if(err) return callback(err);
    callback(null, isMatch);
  });
}

Try changing your localStrategy configuration by this one 尝试通过改变你的localStrategy配置这一项

The default login variable name that express uses is 'username' and 'password'. express使用的默认登录变量名称为“用户名”和“密码”。 In case they have to be changed, as it is 'email' in the above case then the code should be modified in the following way: 如果必须更改它们,因为在上述情况下为“ email”,则应按以下方式修改代码:

passport.use(new localStrategy({usernameField: 'email'}, function(username, password, done){
  User.getUserByEmail(username, function(err, user){
  //rest of the code

Without the change in the usernamefield, the localStrategy searches for 'username' but it does not find it hence, it redirects. 如果没有在用户名字段中进行更改,则localStrategy会搜索“用户名”,但找不到它,因此它将重定向。 Now when the usernameField is changed, it finds the 'email' and uses this in place of username to do the authentication. 现在,当usernameField更改时,它将找到“电子邮件”,并使用它代替username进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM