我已经看过这里问题的其他答案,但仍然一无所获。我无法使comparePassword函数返回true
module.exports.createUser = function(newUser, callback) {
bcrypt.genSalt(10, function(err, salt){
bcrypt.hash(newUser.password, salt, function(err, hash){
newUser.password = salt;
newUser.save(callback);
});
});
};
module.exports.comparePassword = function(candidatePassword, hash, callback){
console.log("Provided password is " + candidatePassword);
console.log("Provided hash is " + hash);
bcrypt.compare(candidatePassword, hash, function(err, isMatch) {
if(err) throw err;
console.log(isMatch);
callback(null, isMatch);
});
}因此,如果我们在此阶段测试用户,您可以看到他们的数据
{ _id: 5aec6f702a4a181f261a43fe,
full_name: 'Its me',
username: 'myusername',
email: 'myemail@gmail.com',
tel_number: '12345678',
password: '$2a$10$6GCgZDt.FL/eeZ1NsDASe.', // text version = test
__v: 0
}运行comparePassword时,控制台日志返回
Provided password is test
Provided hash is $2a$10$6GCgZDt.FL/eeZ1NsDASe.所以对我来说,它们是匹配的,对吧?
不知道这是怎么回事。
发布于 2018-05-06 22:29:51
您可以尝试用newUser.password = hash;替换newUser.password = salt;行吗?
如果有效,请让我们知道。
我的github存储库中有一个示例,它可能会有所帮助
https://github.com/shirshendubhowmick/jwt-demo
以下是代码库中的代码片段
bcrypt.genSalt(10, (error, salt) => {
bcrypt.hash(user.password, salt, (error, hash) => {
user.password = hash;
next();
});
});https://stackoverflow.com/questions/50178904
复制相似问题