Skip to content

Commit 17fb188

Browse files
Support getAuth() after getDatabase()
1 parent 6e35e53 commit 17fb188

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

packages/database/src/core/AppCheckTokenProvider.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,25 @@ export class AppCheckTokenProvider {
3636
) {
3737
this.appCheck = appCheckProvider?.getImmediate({ optional: true });
3838
if (!this.appCheck) {
39-
appCheckProvider?.get().then(appCheck => (this.appCheck = appCheck));
39+
appCheckProvider?.onInit(appCheck => (this.appCheck = appCheck));
4040
}
4141
}
4242

4343
getToken(forceRefresh?: boolean): Promise<AppCheckTokenResult> {
4444
if (!this.appCheck) {
45-
return Promise.resolve(null);
45+
return new Promise<AppCheckTokenResult>((resolve, reject) => {
46+
// Support delayed initialization of FirebaseAppCheck. This allows our
47+
// customers to initialize the RTDB SDK before initializing Firebase
48+
// AppCheck and ensures that all requests are authenticated if a token
49+
// becomes available before the timoeout below expires.
50+
setTimeout(() => {
51+
if (this.appCheck) {
52+
this.getToken(forceRefresh).then(resolve, reject);
53+
} else {
54+
resolve(null);
55+
}
56+
}, 0);
57+
});
4658
}
4759
return this.appCheck.getToken(forceRefresh);
4860
}

packages/database/src/core/AuthTokenProvider.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,33 @@ export interface AuthTokenProvider {
3636
*/
3737
export class FirebaseAuthTokenProvider implements AuthTokenProvider {
3838
private auth_: FirebaseAuthInternal | null = null;
39+
3940
constructor(
4041
private appName_: string,
4142
private firebaseOptions_: object,
4243
private authProvider_: Provider<FirebaseAuthInternalName>
4344
) {
4445
this.auth_ = authProvider_.getImmediate({ optional: true });
4546
if (!this.auth_) {
46-
authProvider_.get().then(auth => (this.auth_ = auth));
47+
authProvider_.onInit(auth => (this.auth_ = auth));
4748
}
4849
}
4950

5051
getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData> {
5152
if (!this.auth_) {
52-
return Promise.resolve(null);
53+
return new Promise<FirebaseAuthTokenData>((resolve, reject) => {
54+
// Support delayed initialization of FirebaseAuth. This allows our
55+
// customers to initialize the RTDB SDK before initializing Firebase
56+
// Auth and ensures that all requests are authenticated if a token
57+
// becomes available before the timoeout below expires.
58+
setTimeout(() => {
59+
if (this.auth_) {
60+
this.getToken(forceRefresh).then(resolve, reject);
61+
} else {
62+
resolve(null);
63+
}
64+
}, 0);
65+
});
5366
}
5467

5568
return this.auth_.getToken(forceRefresh).catch(error => {
@@ -70,7 +83,6 @@ export class FirebaseAuthTokenProvider implements AuthTokenProvider {
7083
if (this.auth_) {
7184
this.auth_.addAuthTokenListener(listener);
7285
} else {
73-
setTimeout(() => listener(null), 0);
7486
this.authProvider_
7587
.get()
7688
.then(auth => auth.addAuthTokenListener(listener));

packages/database/src/core/PersistentConnection.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ export class PersistentConnection extends ServerActions {
243243

244244
return deferred.promise;
245245
}
246+
246247
listen(
247248
query: QueryContext,
248249
currentHashFn: () => string,
@@ -348,6 +349,7 @@ export class PersistentConnection extends ServerActions {
348349
}
349350
}
350351
}
352+
351353
refreshAuthToken(token: string) {
352354
this.authToken_ = token;
353355
this.log_('Auth token refreshed');
@@ -485,6 +487,7 @@ export class PersistentConnection extends ServerActions {
485487

486488
this.sendRequest(action, req);
487489
}
490+
488491
onDisconnectPut(
489492
pathString: string,
490493
data: unknown,
@@ -501,6 +504,7 @@ export class PersistentConnection extends ServerActions {
501504
});
502505
}
503506
}
507+
504508
onDisconnectMerge(
505509
pathString: string,
506510
data: unknown,
@@ -517,6 +521,7 @@ export class PersistentConnection extends ServerActions {
517521
});
518522
}
519523
}
524+
520525
onDisconnectCancel(
521526
pathString: string,
522527
onComplete?: (a: string, b: string) => void
@@ -552,6 +557,7 @@ export class PersistentConnection extends ServerActions {
552557
}
553558
});
554559
}
560+
555561
put(
556562
pathString: string,
557563
data: unknown,
@@ -560,6 +566,7 @@ export class PersistentConnection extends ServerActions {
560566
) {
561567
this.putInternal('p', pathString, data, onComplete, hash);
562568
}
569+
563570
merge(
564571
pathString: string,
565572
data: unknown,
@@ -627,6 +634,7 @@ export class PersistentConnection extends ServerActions {
627634
}
628635
});
629636
}
637+
630638
reportStats(stats: { [k: string]: unknown }) {
631639
// If we're not connected, we just drop the stats.
632640
if (this.connected_) {

0 commit comments

Comments
 (0)