Skip to content

Support getAuth() after getDatabase() #4889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/database/src/core/AppCheckTokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppCheck does not use Provider.initialize() yet, so we need to use get() instead of onInit() for now. We do need to change AppCheck to use Provider.initialize() and change here accordingly, but let's do that in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted this part for now.

}
}

getToken(forceRefresh?: boolean): Promise<AppCheckTokenResult> {
if (!this.appCheck) {
return Promise.resolve(null);
return new Promise<AppCheckTokenResult>((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);
}
Expand Down
18 changes: 15 additions & 3 deletions packages/database/src/core/AuthTokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,33 @@ export interface AuthTokenProvider {
*/
export class FirebaseAuthTokenProvider implements AuthTokenProvider {
private auth_: FirebaseAuthInternal | null = null;

constructor(
private appName_: string,
private firebaseOptions_: object,
private authProvider_: Provider<FirebaseAuthInternalName>
) {
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<FirebaseAuthTokenData> {
if (!this.auth_) {
return Promise.resolve(null);
return new Promise<FirebaseAuthTokenData>((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 => {
Expand All @@ -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));
Expand Down
8 changes: 8 additions & 0 deletions packages/database/src/core/PersistentConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export class PersistentConnection extends ServerActions {

return deferred.promise;
}

listen(
query: QueryContext,
currentHashFn: () => string,
Expand Down Expand Up @@ -348,6 +349,7 @@ export class PersistentConnection extends ServerActions {
}
}
}

refreshAuthToken(token: string) {
this.authToken_ = token;
this.log_('Auth token refreshed');
Expand Down Expand Up @@ -485,6 +487,7 @@ export class PersistentConnection extends ServerActions {

this.sendRequest(action, req);
}

onDisconnectPut(
pathString: string,
data: unknown,
Expand All @@ -501,6 +504,7 @@ export class PersistentConnection extends ServerActions {
});
}
}

onDisconnectMerge(
pathString: string,
data: unknown,
Expand All @@ -517,6 +521,7 @@ export class PersistentConnection extends ServerActions {
});
}
}

onDisconnectCancel(
pathString: string,
onComplete?: (a: string, b: string) => void
Expand Down Expand Up @@ -552,6 +557,7 @@ export class PersistentConnection extends ServerActions {
}
});
}

put(
pathString: string,
data: unknown,
Expand All @@ -560,6 +566,7 @@ export class PersistentConnection extends ServerActions {
) {
this.putInternal('p', pathString, data, onComplete, hash);
}

merge(
pathString: string,
data: unknown,
Expand Down Expand Up @@ -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_) {
Expand Down