我已经尝试过 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);
};
更新 这个问题现在似乎在我的其他项目中已经消失了,设置完全相同。
你不能使用 return 的 await 函数,试试吧
userSchema.methods.checkPassword = async function (inputPass, hashedPass) {
let isValid = await bcrypt.compare(inputPass, hashedPass);
return isValid;
};
我遇到了确切的问题。每次
bcrypt.compare()
返回false。
我通过从数据库中记录原始散列密码和用户输入纯密码来仔细检查它,并在函数之前和之后记录它们。两种情况下的散列密码相同,但.compare()
的返回值始终为false。
如果您找到解决方案,请告诉我。