简体   繁体   English

Bcrypt NodeJS 比较密码总是返回 false

[英]Bcrypt NodeJS compare password always returning false

So obviously i am using bcrypt to register and hash my password and that works.很明显,我正在使用 bcrypt 来注册和散列我的密码,这很有效。 And now i am making an login where i have to compare the form password and the hashed password in the database and this is how i do this:现在我正在登录,我必须在数据库中比较表单密码和散列密码,这就是我这样做的方式:

// Get post data
let email = req.body.email;
let password = req.body.password;

con.query("SELECT * FROM users WHERE email = ?", email, function (err, result, fields) {
    bcrypt.compare(password, result[0].password, function(err, result) {
        if (err) { throw (err); }
        console.log(result);
    });
});

it always returns false;它总是返回 false; if i console.log result[0].password, i get my hashed password back so thats good but why does it always return false?如果我使用console.log result[0].password,我会取回我的散列密码,这样很好,但为什么它总是返回false?

I would suggest you to try changing your if statement to:我建议您尝试将 if 语句更改为:

con.query("SELECT * FROM users WHERE email = ?", email, function (err,
     result, fields) {
         bcrypt.compare(password, result[0].password, function(err, result) {
             if (!result) { throw (err); }
             console.log(result);
         }); 
    });

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

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