From 17fb1884d08b0f0d1d30c5f0823db0d5fe54b7ad Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Mon, 10 May 2021 14:28:33 -0600 Subject: [PATCH 1/2] Support getAuth() after getDatabase() --- .../database/src/core/AppCheckTokenProvider.ts | 16 ++++++++++++++-- .../database/src/core/AuthTokenProvider.ts | 18 +++++++++++++++--- .../database/src/core/PersistentConnection.ts | 8 ++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/packages/database/src/core/AppCheckTokenProvider.ts b/packages/database/src/core/AppCheckTokenProvider.ts index b50f147b5a5..1de2c602d7b 100644 --- a/packages/database/src/core/AppCheckTokenProvider.ts +++ b/packages/database/src/core/AppCheckTokenProvider.ts @@ -36,13 +36,25 @@ export class AppCheckTokenProvider { ) { this.appCheck = appCheckProvider?.getImmediate({ optional: true }); if (!this.appCheck) { - appCheckProvider?.get().then(appCheck => (this.appCheck = appCheck)); + appCheckProvider?.onInit(appCheck => (this.appCheck = appCheck)); } } getToken(forceRefresh?: boolean): Promise { if (!this.appCheck) { - return Promise.resolve(null); + return new Promise((resolve, reject) => { + // Support delayed initialization of FirebaseAppCheck. This allows our + // customers to initialize the RTDB SDK before initializing Firebase + // AppCheck and ensures that all requests are authenticated if a token + // becomes available before the timoeout below expires. + setTimeout(() => { + if (this.appCheck) { + this.getToken(forceRefresh).then(resolve, reject); + } else { + resolve(null); + } + }, 0); + }); } return this.appCheck.getToken(forceRefresh); } diff --git a/packages/database/src/core/AuthTokenProvider.ts b/packages/database/src/core/AuthTokenProvider.ts index 0feb61975a8..46ccf9b0a68 100644 --- a/packages/database/src/core/AuthTokenProvider.ts +++ b/packages/database/src/core/AuthTokenProvider.ts @@ -36,6 +36,7 @@ export interface AuthTokenProvider { */ export class FirebaseAuthTokenProvider implements AuthTokenProvider { private auth_: FirebaseAuthInternal | null = null; + constructor( private appName_: string, private firebaseOptions_: object, @@ -43,13 +44,25 @@ export class FirebaseAuthTokenProvider implements AuthTokenProvider { ) { this.auth_ = authProvider_.getImmediate({ optional: true }); if (!this.auth_) { - authProvider_.get().then(auth => (this.auth_ = auth)); + authProvider_.onInit(auth => (this.auth_ = auth)); } } getToken(forceRefresh: boolean): Promise { if (!this.auth_) { - return Promise.resolve(null); + return new Promise((resolve, reject) => { + // Support delayed initialization of FirebaseAuth. This allows our + // customers to initialize the RTDB SDK before initializing Firebase + // Auth and ensures that all requests are authenticated if a token + // becomes available before the timoeout below expires. + setTimeout(() => { + if (this.auth_) { + this.getToken(forceRefresh).then(resolve, reject); + } else { + resolve(null); + } + }, 0); + }); } return this.auth_.getToken(forceRefresh).catch(error => { @@ -70,7 +83,6 @@ export class FirebaseAuthTokenProvider implements AuthTokenProvider { if (this.auth_) { this.auth_.addAuthTokenListener(listener); } else { - setTimeout(() => listener(null), 0); this.authProvider_ .get() .then(auth => auth.addAuthTokenListener(listener)); diff --git a/packages/database/src/core/PersistentConnection.ts b/packages/database/src/core/PersistentConnection.ts index d700cb0fe43..68be2a68b26 100644 --- a/packages/database/src/core/PersistentConnection.ts +++ b/packages/database/src/core/PersistentConnection.ts @@ -243,6 +243,7 @@ export class PersistentConnection extends ServerActions { return deferred.promise; } + listen( query: QueryContext, currentHashFn: () => string, @@ -348,6 +349,7 @@ export class PersistentConnection extends ServerActions { } } } + refreshAuthToken(token: string) { this.authToken_ = token; this.log_('Auth token refreshed'); @@ -485,6 +487,7 @@ export class PersistentConnection extends ServerActions { this.sendRequest(action, req); } + onDisconnectPut( pathString: string, data: unknown, @@ -501,6 +504,7 @@ export class PersistentConnection extends ServerActions { }); } } + onDisconnectMerge( pathString: string, data: unknown, @@ -517,6 +521,7 @@ export class PersistentConnection extends ServerActions { }); } } + onDisconnectCancel( pathString: string, onComplete?: (a: string, b: string) => void @@ -552,6 +557,7 @@ export class PersistentConnection extends ServerActions { } }); } + put( pathString: string, data: unknown, @@ -560,6 +566,7 @@ export class PersistentConnection extends ServerActions { ) { this.putInternal('p', pathString, data, onComplete, hash); } + merge( pathString: string, data: unknown, @@ -627,6 +634,7 @@ export class PersistentConnection extends ServerActions { } }); } + reportStats(stats: { [k: string]: unknown }) { // If we're not connected, we just drop the stats. if (this.connected_) { From 7ccc471840b74c6006ecc9652d850b36637370e6 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 11 May 2021 15:06:25 -0600 Subject: [PATCH 2/2] Revernt init --- packages/database/src/core/AppCheckTokenProvider.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/database/src/core/AppCheckTokenProvider.ts b/packages/database/src/core/AppCheckTokenProvider.ts index 1de2c602d7b..5856fa4d06a 100644 --- a/packages/database/src/core/AppCheckTokenProvider.ts +++ b/packages/database/src/core/AppCheckTokenProvider.ts @@ -36,7 +36,7 @@ export class AppCheckTokenProvider { ) { this.appCheck = appCheckProvider?.getImmediate({ optional: true }); if (!this.appCheck) { - appCheckProvider?.onInit(appCheck => (this.appCheck = appCheck)); + appCheckProvider?.get().then(appCheck => (this.appCheck = appCheck)); } }