@@ -266,7 +266,7 @@ userSchema.statics.findByUsername = function findByUsername(
266
266
267
267
/**
268
268
*
269
- * Queries User collection using email or username with optional callback .
269
+ * Queries User collection using email or username.
270
270
* This function will determine automatically whether the data passed is
271
271
* a username or email, unless you specify options.valueType
272
272
*
@@ -276,37 +276,30 @@ userSchema.statics.findByUsername = function findByUsername(
276
276
* default query for username or email, defaults
277
277
* to false
278
278
* @param {("email"|"username") } options.valueType - Prevents automatic type inferrence
279
- * @callback [cb] - Optional error-first callback that passes User document
280
279
* @return {Promise<Object> } - Returns Promise fulfilled by User document
281
280
*/
282
281
userSchema . statics . findByEmailOrUsername = function findByEmailOrUsername (
283
282
value ,
284
- options ,
285
- cb
283
+ options
286
284
) {
287
- let isEmail ;
288
- if ( options && options . valueType ) {
289
- isEmail = options . valueType === 'email' ;
290
- } else {
291
- isEmail = value . indexOf ( '@' ) > - 1 ;
292
- }
285
+ const isEmail = options ?. valueType
286
+ ? options . valueType === 'email'
287
+ : value . includes ( '@' ) ;
288
+
289
+ const query = isEmail ? { email : value } : { username : value } ;
290
+ const queryOptions = {
291
+ collation : { locale : 'en' , strength : 2 } ,
292
+ maxTimeMS : 10000 // Set a timeout of 10 seconds to help prevent long-running queries
293
+ } ;
294
+ const queryPromise = this . findOne ( query , queryOptions ) . exec ( ) ;
295
+
293
296
// do the case insensitive stuff
294
- if (
295
- ( arguments . length === 3 && options . caseInsensitive ) ||
296
- ( arguments . length === 2 &&
297
- typeof options === 'object' &&
298
- options . caseInsensitive )
299
- ) {
300
- const query = isEmail ? { email : value } : { username : value } ;
301
- return this . findOne ( query )
302
- . collation ( { locale : 'en' , strength : 2 } )
303
- . exec ( cb ) ;
304
- }
305
- const callback = typeof options === 'function' ? options : cb ;
306
- if ( isEmail ) {
307
- return this . findByEmail ( value , callback ) ;
308
- }
309
- return this . findByUsername ( value , callback ) ;
297
+ // TODO: Handling options should be figured out. At the moment, I think scenarios where it's currently used can be case insensitive?
298
+ // if (options?.caseInsensitive) {
299
+ // return queryPromise;
300
+ // }
301
+
302
+ return queryPromise ;
310
303
} ;
311
304
312
305
/**
0 commit comments