-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
user.js
161 lines (131 loc) · 3.34 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict';
/**
* Module dependencies.
*/
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const Schema = mongoose.Schema;
const oAuthTypes = ['github', 'twitter', 'google', 'linkedin'];
/**
* User Schema
*/
const UserSchema = new Schema({
name: { type: String, default: '' },
email: { type: String, default: '' },
username: { type: String, default: '' },
provider: { type: String, default: '' },
hashed_password: { type: String, default: '' },
authToken: { type: String, default: '' },
twitter: {},
github: {},
google: {},
linkedin: {}
});
const validatePresenceOf = value => value && value.length;
/**
* Virtuals
*/
UserSchema.virtual('password')
.set(function(password) {
this._password = password;
this.hashed_password = this.encryptPassword(password);
})
.get(function() {
return this._password;
});
/**
* Validations
*/
// the below 5 validations only apply if you are signing up traditionally
UserSchema.path('name').validate(function(name) {
if (this.skipValidation()) return true;
return name.length;
}, 'Name cannot be blank');
UserSchema.path('email').validate(function(email) {
if (this.skipValidation()) return true;
return email.length;
}, 'Email cannot be blank');
UserSchema.path('email').validate(function(email) {
return new Promise(resolve => {
const User = mongoose.model('User');
if (this.skipValidation()) return resolve(true);
// Check only when it is a new user or when email field is modified
if (this.isNew || this.isModified('email')) {
User.find({ email }).exec((err, users) => resolve(!err && !users.length));
} else resolve(true);
});
}, 'Email `{VALUE}` already exists');
UserSchema.path('username').validate(function(username) {
if (this.skipValidation()) return true;
return username.length;
}, 'Username cannot be blank');
UserSchema.path('hashed_password').validate(function(hashed_password) {
if (this.skipValidation()) return true;
return hashed_password.length && this._password.length;
}, 'Password cannot be blank');
/**
* Pre-save hook
*/
UserSchema.pre('save', function(next) {
if (!this.isNew) return next();
if (!validatePresenceOf(this.password) && !this.skipValidation()) {
next(new Error('Invalid password'));
} else {
next();
}
});
/**
* Methods
*/
UserSchema.methods = {
/**
* Authenticate - check if the passwords are the same
*
* @param {String} password
* @return {Boolean}
* @api public
*/
authenticate: function(password) {
return bcrypt.compareSync(password, this.hashed_password);
},
/**
* Encrypt password
*
* @param {String} password
* @return {String}
* @api public
*/
encryptPassword: function(password) {
if (!password) return '';
try {
return bcrypt.hashSync(password, 10);
} catch (err) {
return '';
}
},
/**
* Validation is not required if using OAuth
*/
skipValidation: function() {
return ~oAuthTypes.indexOf(this.provider);
}
};
/**
* Statics
*/
UserSchema.statics = {
/**
* Load
*
* @param {Object} options
* @param {Function} cb
* @api private
*/
load: function(options, cb) {
options.select = options.select || 'name username';
return this.findOne(options.criteria)
.select(options.select)
.exec(cb);
}
};
mongoose.model('User', UserSchema);