Bcrypt node.js returns false on compare

╄→гoц情女王★ 提交于 2019-12-25 04:26:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!