-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
53 lines (43 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
46
47
48
49
50
51
52
53
/**
* Publications
* User Model
* 2014 Michael Kelly and Carlos Paelinck
*/
var bcrypt = require('bcryptjs'),
mongoose = require('mongoose');
var Schema = mongoose.Schema,
kSaltFactor = 10;
var userSchema = new Schema({
name: { type: String, required: true, trim: true, unique: true },
password: { type: String, required: true },
modified: { type: Date, 'default': Date.now },
temporary: { type: Boolean },
documents: [{ type: Schema.Types.ObjectId, ref: 'Document' }]
});
userSchema.pre('save', function(next) {
var user = this;
if (!user.isModified('password')) return next();
bcrypt.genSalt(kSaltFactor, 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 = {
validatePassword: function(password, cb) {
bcrypt.compare(password, this.password, function(err, isValid) {
if (err) return cb(err);
cb(null, isValid);
});
},
toJSON: function() {
var obj = this.toObject();
delete obj.password;
delete obj.__v;
return obj;
}
};
module.exports = mongoose.model('User', userSchema);