【发布时间】:2018-12-19 20:35:36
【问题描述】:
我正在制作用户授权系统,并希望在将密码保存到数据库之前对其进行哈希处理。为此,我使用 bcrypt-nodejs。 上面标题中的问题;
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var userSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true,
},
username: {
type: String,
unique: true,
required: true
},
password: {
type: String,
unique: true,
required: true
}
});
userSchema.pre('save', (next) => {
var user = this;
bcrypt.hash(user.password, bcrypt.genSaltSync(10), null, (err, hash) => {
if (err) {
return next(err);
}
user.password = hash;
next();
})
});
module.exports = mongoose.model('User', userSchema);
【问题讨论】:
标签: javascript node.js asp.net-web-api web bcrypt