简体   繁体   中英

bcrypt.compare ( ) always returning false

I have tried both bcrypt (version 5.0.0) and bcryptjs (version 2.4.3) to hash and compare passwords. But the compare function in both the packages always returns false .

Hashing

userSchema.pre('save', async function (next) {
  this.password = await bcrypt.hash(this.password, 12);
  this.passwordConfirm = undefined;
  next();
});

Comparing

userSchema.methods.checkPassword = async function (inputPass, hashedPass) {
  return await bcrypt.compare(inputPass, hashedPass);
};

you can't using await function with return, just try

userSchema.methods.checkPassword = async function (inputPass, hashedPass) {
  let isValid = await bcrypt.compare(inputPass, hashedPass);
  return isValid;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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