@@ -157,7 +157,7 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
157157
158158 private forceRefresh = false ;
159159
160- private auth ? : FirebaseAuthInternal ;
160+ private auth : FirebaseAuthInternal | null = null ;
161161
162162 constructor ( authProvider : Provider < FirebaseAuthInternalName > ) {
163163 this . tokenListener = ( ) => {
@@ -171,24 +171,31 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
171171
172172 this . tokenCounter = 0 ;
173173
174- void authProvider . get ( ) . then ( auth => {
174+ const registerAuth = ( auth : FirebaseAuthInternal ) => {
175175 logDebug ( 'FirebaseCredentialsProvider' , 'Auth detected' ) ;
176176 this . auth = auth ;
177177 if ( this . tokenListener ) {
178178 // tokenListener can be removed by removeChangeListener()
179179 this . auth . addAuthTokenListener ( this . tokenListener ) ;
180180 }
181- } ) ;
181+ } ;
182+
183+ void authProvider . get ( ) . then ( auth => registerAuth ( auth ) ) ;
182184
183185 // Our users can initialize Auth right after Firestore, so we give it
184186 // a chance to register itself with the component framework before we
185187 // determine whether to start up in unauthenticated mode.
186188 setTimeout ( ( ) => {
187- // If auth is still not available, invoke tokenListener once with null
188- // token
189- if ( ! this . auth && this . tokenListener ) {
190- logDebug ( 'FirebaseCredentialsProvider' , 'Auth not yet detected' ) ;
191- this . tokenListener ( null ) ;
189+ if ( ! this . auth ) {
190+ const auth = authProvider . getImmediate ( { optional : true } ) ;
191+ if ( auth ) {
192+ registerAuth ( auth ) ;
193+ } else if ( this . tokenListener ) {
194+ // If auth is still not available, invoke tokenListener once with null
195+ // token
196+ logDebug ( 'FirebaseCredentialsProvider' , 'Auth not yet detected' ) ;
197+ this . tokenListener ( null ) ;
198+ }
192199 }
193200 } , 0 ) ;
194201 }
0 commit comments