@@ -266,7 +266,7 @@ userSchema.statics.findByUsername = function findByUsername(
266266
267267/**
268268 *
269- * Queries User collection using email or username.
269+ * Queries User collection using email or username with optional callback .
270270 * This function will determine automatically whether the data passed is
271271 * a username or email, unless you specify options.valueType
272272 *
@@ -276,30 +276,37 @@ userSchema.statics.findByUsername = function findByUsername(
276276 * default query for username or email, defaults
277277 * to false
278278 * @param {("email"|"username") } options.valueType - Prevents automatic type inferrence
279+ * @callback [cb] - Optional error-first callback that passes User document
279280 * @return {Promise<Object> } - Returns Promise fulfilled by User document
280281 */
281282userSchema . statics . findByEmailOrUsername = function findByEmailOrUsername (
282283 value ,
283- options
284+ options ,
285+ cb
284286) {
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+ }
296293 // 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 ) ;
303310} ;
304311
305312/**
0 commit comments