问题
i got a problem with bcrypt on node.js -> the bcrypt compare method returns false while comparing the passwords from a mongoDB and a user entry. I have no idea why and i tried to debug it since 3 days but i really need some extern help...
UserShema:
var userSchema = new Schema({
name: {
type: String,
required: true
},
password: {
type: String,
required: true
}
});
hash and salt:
userSchema.pre('save', function (next) {
var user = this;
if (this.isModified('password') || this.isNew) {
bcrypt.genSalt(10, function (err, salt) {
if (err) {
return next(err);
}
bcrypt.hash(user.password, salt, function (err, hash) {
if (err) {
return next(err);
}
user.password = hash;
next();
});
});
} else {
return next();
}
});
and finally: the compare method:
userSchema.methods.comparePassword = function (passw, cb) {
bcrypt.compare(passw, this.password, function (err, isMatch) {
if (err) {
return cb(err);
}
cb(null, isMatch);
});
};
module.exports = mongoose.model('User', userSchema);
Thx alot!
来源:https://stackoverflow.com/questions/40558748/bcrypt-node-js-returns-false-on-compare