Skip to content

Commit

Permalink
feat: extend authentication to support id token change event (#710)
Browse files Browse the repository at this point in the history
* refactors handleIdTokenChange on the web
  • Loading branch information
ebarooni committed Nov 6, 2024
1 parent c22b878 commit 43c1fe4
Showing 1 changed file with 14 additions and 27 deletions.
41 changes: 14 additions & 27 deletions packages/authentication/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ import type {
GetIdTokenOptions,
GetIdTokenResult,
GetTenantIdResult,
IdTokenChange,
IsSignInWithEmailLinkOptions,
IsSignInWithEmailLinkResult,
LinkResult,
Expand Down Expand Up @@ -131,7 +130,7 @@ export class FirebaseAuthenticationWeb
super();
const auth = getAuth();
auth.onAuthStateChanged(user => this.handleAuthStateChange(user));
auth.onIdTokenChanged(user => this.handleIdTokenChange(user));
auth.onIdTokenChanged(() => void this.handleIdTokenChange());
}

public async applyActionCode(options: ApplyActionCodeOptions): Promise<void> {
Expand Down Expand Up @@ -793,33 +792,21 @@ export class FirebaseAuthenticationWeb
);
}

private handleIdTokenChange(user: FirebaseUser | null): void {
const change: IdTokenChange = {
token: null,
private async handleIdTokenChange(): Promise<void> {
const change: GetIdTokenResult = {
token: '',
};
if (user) {
void user
?.getIdToken()
.then(token => {
change.token = token;
this.notifyListeners(
FirebaseAuthenticationWeb.ID_TOKEN_CHANGE_EVENT,
change,
true,
);
})
.catch(() => {
this.notifyListeners(
FirebaseAuthenticationWeb.ID_TOKEN_CHANGE_EVENT,
change,
);
});
} else {
this.notifyListeners(
FirebaseAuthenticationWeb.ID_TOKEN_CHANGE_EVENT,
change,
);
try {
const { token } = await this.getIdToken({ forceRefresh: false });
change.token = token;
} catch {
change.token = '';
}
this.notifyListeners(
FirebaseAuthenticationWeb.ID_TOKEN_CHANGE_EVENT,
change,
true,
);
}

private applySignInOptions(
Expand Down

0 comments on commit 43c1fe4

Please sign in to comment.