forked from angular-fullstack/generator-angular-fullstack
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser.model.js
205 lines (176 loc) · 4.69 KB
/
user.model.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var crypto = require('crypto');
var CredentialSchema = new Schema({
type: {
type: String,
"default": 'email',
"enum": ['email', 'phone']
},
value: {
type: String,
required: true,
lowercase: true,
trim: true
},
confirmed: {
type: Boolean,
"default": false
}
}, { _id:false });
var UserSchema = new Schema({
name: String,
role: {
type: String,
"default": 'user'
},
username: String,
salt: String,
hashedPassword: String,
credentials: [ CredentialSchema ]<% if (filters.oauth) { %>,
// NOTE: using `Mixed` is tricky. Should be changed to sth else.
strategies: {
type: Schema.Types.Mixed,
"default": {}
},
localEnabled: {
type: Boolean,
"default": false
}<% } %>
});
UserSchema
.virtual('password')
.set(function(pwd) {
this.salt = this.makeSalt();
this.hashedPassword = this.encryptPassword(pwd);
<% if (filters.oauth) { %>// Setting password implies enabling LocalStrategy
this.localEnabled = true;<% } %>
});
UserSchema
.path('hashedPassword')<% if (filters.oauth) { %>
.validate(function(hashedPwd) {
return !!this.emails.length;
}, 'Cannot set password with empty email')<% } %>
.validate(function(hashedPwd) {<% if (filters.oauth) { %>
if (!this.localEnabled) return true;<% } %>
return !!hashedPwd.length;
}, 'Password cannot be blank');
UserSchema
.virtual('email')
.set(function(email) {
this.credentials.push({
value: email
});
}).get(function() {
// returns only first found email
// TODO: in case of multiple emails, should prioritize confirmed ones
return this.credentials.filter(function(c) {
return c.type === 'email';
})[0].value;
});
UserSchema
.virtual('emails')
.get(function() {
return this.credentials
.filter(function(c) { return c.type === 'email'; })
.map(function(c) { return c.value; });
});
UserSchema
.pre('save', function(next) {<% if (filters.oauth) { %>
if(!this.localEnabled) {
if (Object.keys(this.strategies).length === 0) {
return next(new Error('No connected accounts'));
}
return next();
}<% } %>
mongoose.models.User<% if (filters.oauth) { %>
.find({ localEnabled:true })<% } %>
.where('credentials.type').equals('email')
.where('credentials.value').equals(this.email)
.where('_id').ne(String(this._id))
.exec(function(err, users) {
if (users.length) {
return next(new Error('Account with this email address already exists'));
}
next();
});
});
UserSchema.methods = {
authenticate: function(pwd) {
return this.hashedPassword === this.encryptPassword(pwd);
},
encryptPassword: function(pwd) {
var salt;
if (!pwd || !this.salt) {
return null;
}
salt = new Buffer(this.salt, 'base64');
return crypto.pbkdf2Sync(pwd, salt, 10000, 64).toString('base64');
},
confirm: function(emailOrPhone, cb) {
this.credentials.forEach(function(c) {
if (c.value === emailOrPhone) {
c.confirmed = true;
}
});
this.save(cb);
},
changeEmail: function(oldEmail, newEmail, cb) {
this.credentials.forEach(function(c) {
if (c.value === oldEmail) {
c.value = newEmail;
c.confirmed = false;
}
});
this.save(cb);
},
makeSalt: function() {
return crypto.randomBytes(16).toString('base64');
}<% if (filters.oauth) { %>,
absorb: function(name, profile) {
if (!this.strategies[name]) {
this.strategies[name] = profile;
this.markModified('strategies');
this.save();
} else {
// TODO: move current to archive, and save current as current
console.log("update profile");
}
}<% } %>
};
UserSchema.statics = {
findOneByEmail: function(email, cb) {
this.find({ 'credentials.value': email.toLowerCase() })
.where('credentials.type').equals('email')
.exec(function(err, user) {
if (err) return cb(err);
if (user.length === 0) return cb(null, null);
cb(null, user[0]);
});
}<% if (filters.oauth) { %>,
findDuplicates: function(data, cb) {
var dataFormatted;
dataFormatted = [];
if (data.email !== null) {
dataFormatted.push({
'credentials.type': 'email',
'credentials.value': data.email
});
}
if (data.phone !== null) {
dataFormatted.push({
'credentials.type': 'phone',
'credentials.value': data.phone
});
}
this.find({ 'credentials.confirmed':true })
.or(dataFormatted)
.exec(function(err, users) {
if (err) return cb(err);
if (users.length === 0) return cb(null, null);
cb(null, users);
});
}<% } %>
};
module.exports = mongoose.model('User', UserSchema);