Skip to content

Commit 07ccc28

Browse files
Defer check whether auth is available
1 parent 919413c commit 07ccc28

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

packages/firestore/src/api/credentials.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,26 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
171171

172172
this.tokenCounter = 0;
173173

174-
this.auth = authProvider.getImmediate({ optional: true });
174+
authProvider.get().then(auth => {
175+
logDebug('FirebaseCredentialsProvider', 'Auth detected');
176+
this.auth = auth;
177+
if (this.tokenListener) {
178+
// tokenListener can be removed by removeChangeListener()
179+
this.auth.addAuthTokenListener(this.tokenListener);
180+
}
181+
});
175182

176-
if (this.auth) {
177-
this.auth.addAuthTokenListener(this.tokenListener!);
178-
} else {
179-
// if auth is not available, invoke tokenListener once with null token
180-
this.tokenListener(null);
181-
authProvider.get().then(
182-
auth => {
183-
this.auth = auth;
184-
if (this.tokenListener) {
185-
// tokenListener can be removed by removeChangeListener()
186-
this.auth.addAuthTokenListener(this.tokenListener);
187-
}
188-
},
189-
() => {
190-
/* this.authProvider.get() never rejects */
191-
}
192-
);
193-
}
183+
// Our users can initialize Auth right after Firestore, so we give it
184+
// a chance to register itself with the component framework before we
185+
// determine whether to start up in unauthenticated mode.
186+
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);
192+
}
193+
}, 0);
194194
}
195195

196196
getToken(): Promise<Token | null> {

0 commit comments

Comments
 (0)