Skip to content

Commit 8bece48

Browse files
Support getAuth() after getDatabase() (#4889)
1 parent c85a32d commit 8bece48

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

packages/database/src/core/AppCheckTokenProvider.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,19 @@ export class AppCheckTokenProvider {
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
@@ -244,6 +244,7 @@ export class PersistentConnection extends ServerActions {
244244

245245
return deferred.promise;
246246
}
247+
247248
listen(
248249
query: QueryContext,
249250
currentHashFn: () => string,
@@ -351,6 +352,7 @@ export class PersistentConnection extends ServerActions {
351352
}
352353
}
353354
}
355+
354356
refreshAuthToken(token: string) {
355357
this.authToken_ = token;
356358
this.log_('Auth token refreshed');
@@ -488,6 +490,7 @@ export class PersistentConnection extends ServerActions {
488490

489491
this.sendRequest(action, req);
490492
}
493+
491494
onDisconnectPut(
492495
pathString: string,
493496
data: unknown,
@@ -506,6 +509,7 @@ export class PersistentConnection extends ServerActions {
506509
});
507510
}
508511
}
512+
509513
onDisconnectMerge(
510514
pathString: string,
511515
data: unknown,
@@ -524,6 +528,7 @@ export class PersistentConnection extends ServerActions {
524528
});
525529
}
526530
}
531+
527532
onDisconnectCancel(
528533
pathString: string,
529534
onComplete?: (a: string, b: string) => void
@@ -561,6 +566,7 @@ export class PersistentConnection extends ServerActions {
561566
}
562567
});
563568
}
569+
564570
put(
565571
pathString: string,
566572
data: unknown,
@@ -569,6 +575,7 @@ export class PersistentConnection extends ServerActions {
569575
) {
570576
this.putInternal('p', pathString, data, onComplete, hash);
571577
}
578+
572579
merge(
573580
pathString: string,
574581
data: unknown,
@@ -638,6 +645,7 @@ export class PersistentConnection extends ServerActions {
638645
}
639646
});
640647
}
648+
641649
reportStats(stats: { [k: string]: unknown }) {
642650
// If we're not connected, we just drop the stats.
643651
if (this.connected_) {

0 commit comments

Comments
 (0)