-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
82 lines (74 loc) · 1.52 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var mongoose = require("mongoose");
var bcrypt = require('bcryptjs');
var SALT_WORK_FACTOR = 10;
var UserSchema = new mongoose.Schema({
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
title: {
type: String,
required: true
},
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
address: {
type: String,
required: true
},
phone: {
type: Number,
required: true
},
department: {
type: String,
required: true
},
companyId: {
type: String,
required: true
},
departmentAdmin: {
type: Boolean,
required: false,
default: false
},
admin: {
type: Boolean,
required: false,
default: false
},
systemAdmin: {
type: Boolean,
required: false,
default: false
}
})
UserSchema.pre('save', function(next) {
var users = this;
// only hash the password if it has been modified (or is new)
if (!users.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 our new salt
bcrypt.hash(users.password, salt, function(err, hash) {
if (err) return next(err);
// override the cleartext password with the hashed one
users.password = hash;
next();
});
});
});
var User = mongoose.model("User", UserSchema);
module.exports = User;