使用passport-local和bcrypt在node.js中检查和更新密码

当我尝试在我的节点应用程序中validation用户的现有密码时,出现[Error: data and hash arguments required]错误。 上下文是我要求我的用户在用户configuration文件页面中更改之前validation其现有的密码。 我的堆栈是node + mondodb(通过mongoose)使用passport-local与bcrypt。

相关的代码是:

 // code trying to match that returns the aforementioned error req.user.comparePassword(req.body.password, function (err, isMatch) { if (err) { return console.error(err); } if (isMatch) { console.log('passwords match'); // now save new password // Password verification userSchema.methods.comparePassword = function (candidatePassword, cb) { bcrypt.compare(candidatePassword, this.password, function (err, isMatch) { if (err) return cb(err); cb(null, isMatch); }); }; } } 

req.user引用当前的用户对象,`req.body.password'是从用户的POST获得的密码。 我在这里使用护照本地示例中的UserSchema,护照策略和Bcryptconfiguration。

有人可以提供有关如何在更新之前validation密码是否匹配的指导?

所以bcrypt.compare抱怨其中一个参数, datahash丢失。 所以这意味着也许this.password返回nullundefined 。 检查你的数据库logging,并确保它有一个有效的散列存储。