bcrypt.compare() 总是返回 false

问题描述

我已经尝试了 bcrypt(5.0.0 版)和 bcryptjs(2.4.3 版)来散列和比较密码。但是两个包中的比较函数总是返回 false

散列

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

比较

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

解决方法

你不能用带返回的await函数,试试

int