35 lines
919 B
JavaScript
35 lines
919 B
JavaScript
var mongoose = require('mongoose'),
|
|
bcrypt = require('bcrypt');
|
|
|
|
|
|
var userSchema = new mongoose.Schema({
|
|
username: { type: String, required: true, unique: true },
|
|
email: { type: String, required: true, unique: true },
|
|
password: { type: String, required: true},
|
|
admin: { type: Boolean, required: true }
|
|
});
|
|
|
|
userSchema.pre('save', function(next) {
|
|
var user = this;
|
|
|
|
if (!user.isModified('password')) return next();
|
|
|
|
bcrypt.genSalt(32, 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();
|
|
});
|
|
});
|
|
});
|
|
|
|
userSchema.methods.comparePassword = function(candidatePassword, cb) {
|
|
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
|
|
if(err) return cb(err);
|
|
cb(null, isMatch);
|
|
});
|
|
};
|
|
|
|
module.exports = mongoose.model('User', userSchema); |