简体   繁体   English

bcrypt与nodejs比较始终为false

[英]bcrypt with nodejs compare always false

i always get a false compare with this line of block for unknown reason even when the user password mathes the hashed password i have in my DB. 即使用户密码算了我数据库中的哈希密码,我也总是出于未知原因而与该行代码进行错误比较。 please help. 请帮忙。

        bcrypt.compare(password, user.password , function(error, result) {
          console.log(result==true);
          if (result === true) {
            return callback(null, user);
          } else {
            return callback();
          }
        })
      });
}
// hash password before saving to database
UserSchema.pre('save', function(next) {
  var user = this;
  bcrypt.hash(user.password, 10, function(err, hash) {
    if (err) {
      return next(err);
    }
    user.password = hash;
    next();
  })
});

full project and code can be found here: https://github.com/eladnm/Trinity-Management-System- 完整的项目和代码可以在这里找到: https : //github.com/eladnm/Trinity-Management-System-

Perhaps, result === true returns false as result might not be an actual true value. 也许, result === true返回false,因为结果可能不是实际的true值。 It could be a truthly value and which when compared with == returns true. 它可能是一个真实值,当与==比较时,它返回true。 (From bcrypt's npm ) (来自bcrypt的npm
You could simply write: 您可以简单地写:

if (err) {
  return callback(err);
}
if (result) {
  return callback(null, user);
} 
return callback();

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

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