@@ -266,7 +266,7 @@ userSchema.statics.findByUsername = function findByUsername(
266
266
267
267
/**
268
268
*
269
- * Queries User collection using email or username.
269
+ * Queries User collection using email or username with optional callback .
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,30 +276,37 @@ 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
279
280
* @return {Promise<Object> } - Returns Promise fulfilled by User document
280
281
*/
281
282
userSchema . statics . findByEmailOrUsername = function findByEmailOrUsername (
282
283
value ,
283
- options
284
+ options ,
285
+ cb
284
286
) {
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
-
287
+ let isEmail ;
288
+ if ( options && options . valueType ) {
289
+ isEmail = options . valueType === 'email' ;
290
+ } else {
291
+ isEmail = value . indexOf ( '@' ) > - 1 ;
292
+ }
296
293
// do the case insensitive stuff
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 ;
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 ) ;
303
310
} ;
304
311
305
312
/**
0 commit comments