@@ -130,7 +130,8 @@ describe('core/auth/initializeAuth', () => {
130130 async function initAndWait (
131131 persistence : Persistence | Persistence [ ] ,
132132 popupRedirectResolver ?: PopupRedirectResolver ,
133- authDomain = FAKE_APP . options . authDomain
133+ authDomain = FAKE_APP . options . authDomain ,
134+ blockMiddleware = false ,
134135 ) : Promise < Auth > {
135136 const auth = new AuthImpl ( FAKE_APP , FAKE_HEARTBEAT_CONTROLLER_PROVIDER , {
136137 apiKey : FAKE_APP . options . apiKey ! ,
@@ -146,6 +147,12 @@ describe('core/auth/initializeAuth', () => {
146147 persistence,
147148 popupRedirectResolver
148149 } ) ;
150+
151+ if ( blockMiddleware ) {
152+ auth . beforeAuthStateChanged ( ( ) => {
153+ throw new Error ( 'blocked' ) ;
154+ } ) ;
155+ }
149156 // Auth initializes async. We can make sure the initialization is
150157 // flushed by awaiting a method on the queue.
151158 await auth . setPersistence ( inMemoryPersistence ) ;
@@ -408,6 +415,84 @@ describe('core/auth/initializeAuth', () => {
408415 expect ( user ) . not . to . be . null ;
409416 expect ( auth . currentUser ) . to . eq ( user ) ;
410417 } ) ;
418+
419+ it ( 'does not halt old user load if middleware throws' , async ( ) => {
420+ const stub = sinon . stub (
421+ _getInstance < PersistenceInternal > ( inMemoryPersistence )
422+ ) ;
423+ const oldUser = testUser ( oldAuth , 'old-uid' ) ;
424+ stub . _get . returns ( Promise . resolve ( oldUser . toJSON ( ) ) ) ;
425+ const overrideSpy = sinon . spy ( _getInstance < PopupRedirectResolverInternal > ( browserPopupRedirectResolver ) , '_overrideRedirectResult' ) ;
426+ const auth = await initAndWait (
427+ [ inMemoryPersistence ] ,
428+ browserPopupRedirectResolver ,
429+ FAKE_APP . options . authDomain ,
430+ /* blockMiddleware */ true
431+ ) ;
432+
433+ expect ( auth . currentUser ! . uid ) . to . eq ( oldUser . uid ) ;
434+ expect ( reload . _reloadWithoutSaving ) . to . have . been . called ;
435+ expect ( overrideSpy ) . not . to . have . been . called ;
436+ } ) ;
437+
438+ it ( 'Reloads and uses old user if middleware throws' , async ( ) => {
439+ const stub = sinon . stub (
440+ _getInstance < PersistenceInternal > ( inMemoryPersistence )
441+ ) ;
442+ const oldUser = testUser ( oldAuth , 'old-uid' ) ;
443+ stub . _get . returns ( Promise . resolve ( oldUser . toJSON ( ) ) ) ;
444+ const overrideSpy = sinon . spy ( _getInstance < PopupRedirectResolverInternal > ( browserPopupRedirectResolver ) , '_overrideRedirectResult' ) ;
445+
446+ let user : UserInternal | null = null ;
447+ completeRedirectFnStub . callsFake ( ( auth : AuthInternal ) => {
448+ user = testUser ( auth , 'uid' , '[email protected] ' ) ; 449+ return Promise . resolve (
450+ new UserCredentialImpl ( {
451+ operationType : OperationType . SIGN_IN ,
452+ user,
453+ providerId : null
454+ } )
455+ ) ;
456+ } ) ;
457+
458+ const auth = await initAndWait (
459+ [ inMemoryPersistence ] ,
460+ browserPopupRedirectResolver ,
461+ FAKE_APP . options . authDomain ,
462+ /* blockMiddleware */ true
463+ ) ;
464+ expect ( user ) . not . to . be . null ;
465+ expect ( auth . currentUser ! . uid ) . to . eq ( oldUser . uid ) ;
466+ expect ( reload . _reloadWithoutSaving ) . to . have . been . called ;
467+ expect ( overrideSpy ) . to . have . been . called ;
468+ } ) ;
469+
470+ it ( 'Nulls current user if redirect blocked by middleware' , async ( ) => {
471+ const stub = sinon . stub (
472+ _getInstance < PersistenceInternal > ( inMemoryPersistence )
473+ ) ;
474+ stub . _get . returns ( Promise . resolve ( null ) ) ;
475+ completeRedirectFnStub . callsFake ( ( auth : AuthInternal ) => {
476+ const user = testUser ( auth , 'uid' , '[email protected] ' ) ; 477+ return Promise . resolve (
478+ new UserCredentialImpl ( {
479+ operationType : OperationType . SIGN_IN ,
480+ user,
481+ providerId : null
482+ } )
483+ ) ;
484+ } ) ;
485+
486+ const auth = await initAndWait (
487+ [ inMemoryPersistence ] ,
488+ browserPopupRedirectResolver ,
489+ FAKE_APP . options . authDomain ,
490+ /* blockMiddleware */ true
491+ ) ;
492+ expect ( completeRedirectFnStub ) . to . have . been . called ;
493+ expect ( auth . currentUser ) . to . be . null ;
494+ expect ( reload . _reloadWithoutSaving ) . not . to . have . been . called ;
495+ } ) ;
411496 } ) ;
412497 } ) ;
413498} ) ;
0 commit comments