因为bcrypt安装一直在gyp..所以我用的是bcryptjs
我将用户注册输入的密码,在前台进行了一次加密,后台接收到以后又进行了加密,然后就会导致用户登录密码不匹配。。。是我的写法错了呢,还是本来就是如此?
这是前台加密逻辑
bcrypt.genSalt(10, (err, salt) => {
if (err) {
console.log(err)
} else {
bcrypt.hash(this.password, salt, (err, hash) => {
if (err) {
console.log(err)
} else {
this.$http
.post(config.dev.host + '/user/login', {
username: this.username,
password: hash
})
.then((data) => {
console.log(data)
}, (err) => {
console.log(err)
})
}
})
}
这是后台UserSchema的方法
User.pre('save', function (next) {
if (!this.isNew) {
this.meta.updateAt = Date.now();
}
var user = this;
bcrypt.genSalt(10, function (err, salt) {
if (err) {
next(err);
} else {
bcrypt.hash(user.password, salt, function (err, hash) {
if (err) {
next(err);
} else {
console.log(user.username + ' ' + user.password + ' ' + hash);
user.password = hash;
next();
}
})
}
})
});
User.methods = {
comparePassword: function (_password, cb) {
var user = this;
bcrypt.compare(_password, user.password, function (err, isMatched) {
console.log(_password)
console.log(user.password)
if (err) {
cb(err);
} else {
cb(null, isMatched);
}
})
}
}
前台不需要加密。