-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUser.js
45 lines (35 loc) · 1.21 KB
/
User.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var mongoose = require('mongoose');
var bcrypt = require('bcrypt');
var SALT_WORK_FACTOR = 10;
var UserSchema = new mongoose.Schema({
name: { type: String, required: true, index: { unique: true } },
password: { type: String, required: true },
admin: Boolean
});
UserSchema.pre('save', function(next) {
// only hash the password if it has been modified (or new)
if (!user.isModified('password')) return next();
// generate a salt
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) return next(err);
// hash the password along with the salt
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) return next(err);
// override the cleartext password
user.password = hash;
next();
})
});
});
UserSchema.methods.comparePassword = function(candidatePassword, callback) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) return callback(err);
callback(null, isMatch);
});
}
UserSchema.methods.clean = function() {
var user = this.toObject();
delete user.password;
return user;
};
module.exports = mongoose.model('User', UserSchema);