使用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
抱怨其中一个参数, data
或hash
丢失。 所以这意味着也许this.password
返回null
或undefined
。 检查你的数据库logging,并确保它有一个有效的散列存储。
- 要求Passport.js / Node.js目录(除了一个页面)的身份validation?
- 在处理POST请求时确定谁在Node上使用basicAuth进行了身份validation
- NodeJS / Firebase承诺的范围
- 如何保护TCP连接 – nodejs?
- 客户端的JavaScript代码replace
- 如何从运行在appengine中的NodeJS应用程序访问Google CloudML REST API
- Express:一个路由的基本authentication
- 使用LDAP的Node.js身份validation机制
- NodeJS Passport:通过两种策略multilogin – 序列化问题