forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
374 lines (338 loc) · 9.54 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import mongoose from 'mongoose';
import bcrypt from 'bcryptjs';
const EmailConfirmationStates = {
Verified: 'verified',
Sent: 'sent',
Resent: 'resent'
};
const { Schema } = mongoose;
const apiKeySchema = new Schema(
{
label: { type: String, default: 'API Key' },
lastUsedAt: { type: Date },
hashedKey: { type: String, required: true }
},
{ timestamps: true, _id: true }
);
apiKeySchema.virtual('id').get(function getApiKeyId() {
return this._id.toHexString();
});
/**
* When serialising an APIKey instance, the `hashedKey` field
* should never be exposed to the client. So we only return
* a safe list of fields when toObject and toJSON are called.
*/
function apiKeyMetadata(doc, ret, options) {
return {
id: doc.id,
label: doc.label,
lastUsedAt: doc.lastUsedAt,
createdAt: doc.createdAt
};
}
apiKeySchema.set('toObject', {
transform: apiKeyMetadata
});
apiKeySchema.set('toJSON', {
virtuals: true,
transform: apiKeyMetadata
});
const userSchema = new Schema(
{
name: { type: String, default: '' },
username: { type: String, required: true, unique: true },
password: { type: String },
resetPasswordToken: String,
resetPasswordExpires: Date,
verified: { type: String },
verifiedToken: String,
verifiedTokenExpires: Date,
github: { type: String },
google: { type: String },
email: { type: String, unique: true },
tokens: Array,
apiKeys: { type: [apiKeySchema] },
preferences: {
fontSize: { type: Number, default: 18 },
lineNumbers: { type: Boolean, default: true },
indentationAmount: { type: Number, default: 2 },
isTabIndent: { type: Boolean, default: false },
autosave: { type: Boolean, default: true },
linewrap: { type: Boolean, default: true },
lintWarning: { type: Boolean, default: false },
textOutput: { type: Boolean, default: false },
gridOutput: { type: Boolean, default: false },
theme: { type: String, default: 'light' },
autorefresh: { type: Boolean, default: false },
language: { type: String, default: 'en-US' },
autocloseBracketsQuotes: { type: Boolean, default: true },
autocompleteHinter: { type: Boolean, default: false }
},
totalSize: { type: Number, default: 0 },
cookieConsent: {
type: String,
enum: ['none', 'essential', 'all'],
default: 'none'
},
banned: { type: Boolean, default: false }
},
{ timestamps: true, usePushEach: true }
);
/**
* Password hash middleware.
*/
userSchema.pre('save', function checkPassword(next) {
const user = this;
if (!user.isModified('password')) {
next();
return;
}
bcrypt.genSalt(10, (err, salt) => {
if (err) {
next(err);
return;
}
bcrypt.hash(user.password, salt, (innerErr, hash) => {
if (innerErr) {
next(innerErr);
return;
}
user.password = hash;
next();
});
});
});
/**
* API keys hash middleware
*/
userSchema.pre('save', function checkApiKey(next) {
const user = this;
if (!user.isModified('apiKeys')) {
next();
return;
}
let hasNew = false;
let pendingTasks = 0;
let nextCalled = false;
const done = (err) => {
if (nextCalled) return;
if (err) {
nextCalled = true;
next(err);
return;
}
pendingTasks -= 1;
if (pendingTasks === 0) {
nextCalled = true;
next();
}
};
user.apiKeys.forEach((k) => {
if (k.isNew) {
hasNew = true;
pendingTasks += 1;
bcrypt.genSalt(10, (err, salt) => {
if (err) {
done(err);
}
bcrypt.hash(k.hashedKey, salt, (innerErr, hash) => {
if (innerErr) {
done(innerErr);
}
k.hashedKey = hash;
done();
});
});
}
});
if (!hasNew) {
next();
}
});
userSchema.virtual('id').get(function idToString() {
return this._id.toHexString();
});
userSchema.set('toJSON', {
virtuals: true
});
/**
* Helper method for validating user's password.
* @param {string} candidatePassword
* @return {Promise<boolean>}
*/
userSchema.methods.comparePassword = async function comparePassword(
candidatePassword
) {
if (!this.password) {
return false;
}
try {
return await bcrypt.compare(candidatePassword, this.password);
} catch (error) {
console.error('Password comparison failed!', error);
return false;
}
};
/**
* Helper method for validating a user's api key
*/
userSchema.methods.findMatchingKey = async function findMatchingKey(
candidateKey
) {
let keyObj = { isMatch: false, keyDocument: null };
/* eslint-disable no-restricted-syntax */
for (const k of this.apiKeys) {
try {
/* eslint-disable no-await-in-loop */
const foundOne = await bcrypt.compareSync(candidateKey, k.hashedKey);
if (foundOne) {
keyObj = { isMatch: true, keyDocument: k };
return keyObj;
}
} catch (error) {
console.error('Matching API key not found !');
return keyObj;
}
}
return keyObj;
};
/**
*
* Queries User collection by email and returns one User document.
*
* @param {string|string[]} email - Email string or array of email strings
* @callback [cb] - Optional error-first callback that passes User document
* @return {Object} - Returns User Object fulfilled by User document
*/
userSchema.statics.findByEmail = async function findByEmail(email) {
const user = this;
const query = Array.isArray(email) ? { email: { $in: email } } : { email };
// Email addresses should be case-insensitive unique
// In MongoDB, you must use collation in order to do a case-insensitive query
const userFoundByEmail = await user
.findOne(query)
.collation({ locale: 'en', strength: 2 })
.exec();
return userFoundByEmail;
};
/**
*
* Queries User collection by emails and returns all Users that match.
*
* @param {string[]} emails - Array of email strings
* @return {Promise<Object>} - Returns Promise fulfilled by User document
*/
userSchema.statics.findAllByEmails = async function findAllByEmails(emails) {
const user = this;
const query = {
email: { $in: emails }
};
// Email addresses should be case-insensitive unique
// In MongoDB, you must use collation in order to do a case-insensitive query
const usersFoundByEmails = await user
.find(query)
.collation({ locale: 'en', strength: 2 })
.exec();
return usersFoundByEmails;
};
/**
*
* Queries User collection by username and returns one User document.
*
* @param {string} username - Username string
* @param {Object} [options] - Optional options
* @param {boolean} options.caseInsensitive - Does a caseInsensitive query, defaults to false
* @return {Object} - Returns User Object fulfilled by User document
*/
userSchema.statics.findByUsername = async function findByUsername(
username,
options
) {
const user = this;
const query = {
username
};
if (
arguments.length === 2 &&
typeof options === 'object' &&
options.caseInsensitive
) {
const foundUser = await user
.findOne(query)
.collation({ locale: 'en', strength: 2 })
.exec();
return foundUser;
}
const userFoundByUsername = await user.findOne(query).exec();
return userFoundByUsername;
};
/**
*
* Queries User collection using email or username with optional callback.
* This function will determine automatically whether the data passed is
* a username or email, unless you specify options.valueType
*
* @param {string} value - Email or username
* @param {Object} [options] - Optional options
* @param {boolean} options.caseInsensitive - Does a caseInsensitive query rather than
* default query for username or email, defaults
* to false
* @param {("email"|"username")} options.valueType - Prevents automatic type inferrence
* @return {Object} - Returns User Object fulfilled by User document
*/
userSchema.statics.findByEmailOrUsername = async function findByEmailOrUsername(
value,
options
) {
const user = this;
const isEmail =
options && options.valueType
? options.valueType === 'email'
: value.indexOf('@') > -1;
// do the case insensitive stuff
if (
arguments.length === 2 &&
typeof options === 'object' &&
options.caseInsensitive
) {
const query = isEmail ? { email: value } : { username: value };
const foundUser = await user
.findOne(query)
.collation({ locale: 'en', strength: 2 })
.exec();
return foundUser;
}
if (isEmail) {
const userFoundByEmail = await user.findByEmail(value);
return userFoundByEmail;
}
const userFoundByUsername = await user.findByUsername(value);
return userFoundByUsername;
};
/**
*
* Queries User collection, performing a MongoDB logical or with the email
* and username (i.e. if either one matches, will return the first document).
*
* @param {string} email
* @param {string} username
* @return {Object} - Returns User Object fulfilled by User document
*/
userSchema.statics.findByEmailAndUsername = async function findByEmailAndUsername(
email,
username
) {
const user = this;
const query = {
$or: [{ email }, { username }]
};
const foundUser = await user
.findOne(query)
.collation({ locale: 'en', strength: 2 })
.exec();
return foundUser;
};
userSchema.statics.EmailConfirmation = EmailConfirmationStates;
userSchema.index({ username: 1 }, { collation: { locale: 'en', strength: 2 } });
userSchema.index({ email: 1 }, { collation: { locale: 'en', strength: 2 } });
export default mongoose.models.User || mongoose.model('User', userSchema);