Skip to content

Commit 9e4886d

Browse files
committed
convert remaining methods to async/await in user model
1 parent 1ecae60 commit 9e4886d

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

server/models/user.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,27 @@ userSchema.methods.comparePassword = async function comparePassword(
177177
/**
178178
* Helper method for validating a user's api key
179179
*/
180-
userSchema.methods.findMatchingKey = function findMatchingKey(
181-
candidateKey,
182-
cb
180+
userSchema.methods.findMatchingKey = async function findMatchingKey(
181+
candidateKey
183182
) {
184-
let foundOne = false;
185-
this.apiKeys.forEach((k) => {
186-
if (bcrypt.compareSync(candidateKey, k.hashedKey)) {
187-
foundOne = true;
188-
cb(null, true, k);
183+
let keyObj = { isMatch: false, keyDocument: null };
184+
/* eslint-disable no-restricted-syntax */
185+
for (const k of this.apiKeys) {
186+
try {
187+
/* eslint-disable no-await-in-loop */
188+
const foundOne = await bcrypt.compareSync(candidateKey, k.hashedKey);
189+
190+
if (foundOne) {
191+
keyObj = { isMatch: true, keyDocument: k };
192+
return keyObj;
193+
}
194+
} catch (error) {
195+
console.error('Matching API key not found !');
196+
return keyObj;
189197
}
190-
});
191-
if (!foundOne) cb('Matching API key not found !', false, null);
198+
}
199+
200+
return keyObj;
192201
};
193202

194203
/**
@@ -197,7 +206,7 @@ userSchema.methods.findMatchingKey = function findMatchingKey(
197206
*
198207
* @param {string|string[]} email - Email string or array of email strings
199208
* @callback [cb] - Optional error-first callback that passes User document
200-
* @return {Promise<Object>} - Returns Promise fulfilled by User document
209+
* @return {Object} - Returns User Object fulfilled by User document
201210
*/
202211
userSchema.statics.findByEmail = async function findByEmail(email) {
203212
const user = this;
@@ -240,7 +249,7 @@ userSchema.statics.findAllByEmails = async function findAllByEmails(emails) {
240249
* @param {string} username - Username string
241250
* @param {Object} [options] - Optional options
242251
* @param {boolean} options.caseInsensitive - Does a caseInsensitive query, defaults to false
243-
* @return {Promise<Object>} - Returns Promise fulfilled by User document
252+
* @return {Object} - Returns User Object fulfilled by User document
244253
*/
245254
userSchema.statics.findByUsername = async function findByUsername(
246255
username,
@@ -279,7 +288,7 @@ userSchema.statics.findByUsername = async function findByUsername(
279288
* default query for username or email, defaults
280289
* to false
281290
* @param {("email"|"username")} options.valueType - Prevents automatic type inferrence
282-
* @return {Promise<Object>} - Returns Promise fulfilled by User document
291+
* @return {Object} - Returns User Object fulfilled by User document
283292
*/
284293
userSchema.statics.findByEmailOrUsername = async function findByEmailOrUsername(
285294
value,
@@ -321,18 +330,22 @@ userSchema.statics.findByEmailOrUsername = async function findByEmailOrUsername(
321330
*
322331
* @param {string} email
323332
* @param {string} username
324-
* @callback [cb] - Optional error-first callback that passes User document
325-
* @return {Promise<Object>} - Returns Promise fulfilled by User document
333+
* @return {Object} - Returns User Object fulfilled by User document
326334
*/
327-
userSchema.statics.findByEmailAndUsername = function findByEmailAndUsername(
335+
userSchema.statics.findByEmailAndUsername = async function findByEmailAndUsername(
328336
email,
329-
username,
330-
cb
337+
username
331338
) {
339+
const user = this;
332340
const query = {
333341
$or: [{ email }, { username }]
334342
};
335-
return this.findOne(query).collation({ locale: 'en', strength: 2 }).exec(cb);
343+
const foundUser = await user
344+
.findOne(query)
345+
.collation({ locale: 'en', strength: 2 })
346+
.exec();
347+
348+
return foundUser;
336349
};
337350

338351
userSchema.statics.EmailConfirmation = EmailConfirmationStates;

0 commit comments

Comments
 (0)