-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathuser.js
149 lines (133 loc) · 2.97 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
'use strict';
var mongoose = require('mongoose'),
uniqueValidator = require('mongoose-unique-validator'),
Schema = mongoose.Schema,
bcrypt = require('bcrypt-nodejs');
var authTypes = ['github', 'twitter', 'facebook', 'google'],
SALT_WORK_FACTOR = 10;
/**
* User Schema
*/
var UserSchema = new Schema({
name: String,
email: {
type: String,
unique: true
},
role: {
type: String,
default: 'user'
},
hashedPassword: String,
provider: String,
salt: String,
facebook: {},
twitter: {},
github: {},
google: {}
});
/**
* Virtuals
*/
UserSchema
.virtual('password')
.set(function(password) {
this._password = password;
this.salt = this.makeSalt();
this.hashedPassword = this.encryptPassword(password, this.salt);
})
.get(function() {
return this._password;
});
// Basic info to identify the current authenticated user in the app
UserSchema
.virtual('userInfo')
.get(function() {
return {
'name': this.name,
'role': this.role,
'provider': this.provider
};
});
// Public profile information
UserSchema
.virtual('profile')
.get(function() {
return {
'name': this.name,
'role': this.role
};
});
/**
* Validations
*/
var validatePresenceOf = function(value) {
return value && value.length;
};
// Validate empty email
UserSchema
.path('email')
.validate(function(email) {
// if you are authenticating by any of the oauth strategies, don't validate
if (authTypes.indexOf(this.provider) !== -1) return true;
return email.length;
}, 'Email cannot be blank');
// Validate empty password
UserSchema
.path('hashedPassword')
.validate(function(hashedPassword) {
// if you are authenticating by any of the oauth strategies, don't validate
if (authTypes.indexOf(this.provider) !== -1) return true;
return hashedPassword.length;
}, 'Password cannot be blank');
/**
* Plugins
*/
UserSchema.plugin(uniqueValidator, { message: 'Value is not unique.' });
/**
* Pre-save hook
*/
UserSchema
.pre('save', function(next) {
if (!this.isNew) return next();
if (!validatePresenceOf(this.hashedPassword) && authTypes.indexOf(this.provider) === -1)
next(new Error('Invalid password'));
else
next();
});
/**
* Methods
*/
UserSchema.methods = {
/**
* Authenticate - check if the passwords are the same
*
* @param {String} plainText
* @return {Boolean}
* @api public
*/
authenticate: function(plainText) {
return this.encryptPassword(plainText, this.salt) === this.hashedPassword;
},
/**
* Make salt
*
* @return {String}
* @api public
*/
makeSalt: function() {
return bcrypt.genSaltSync(SALT_WORK_FACTOR);
},
/**
* Encrypt password
*
* @param {String} password
* @return {String}
* @api public
*/
encryptPassword: function(password, salt) {
// hash the password using our new salt
return bcrypt.hashSync(password, salt);
}
};
mongoose.model('User', UserSchema);