Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit d03870c

Browse files
committed
[auth][web] - Update onAuthChange to return an unsubscribe function to stop receiving updates
1 parent f0950bf commit d03870c

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/app/auth/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as firebase from "../../firebase";
2-
import { FirebaseEmailLinkActionCodeSettings, LoginType, User } from "../../firebase";
2+
import { FirebaseEmailLinkActionCodeSettings, LoginType, User, Unsubscribe } from "../../firebase";
33

44
export namespace auth {
55
export class Auth {
@@ -44,10 +44,15 @@ export namespace auth {
4444
}
4545

4646
// Completed will never be called, but it is here to closely follow the web api interface.
47-
public onAuthStateChanged(handler: (user: User) => void, error?: (err) => any, completed?: () => any): void {
47+
public onAuthStateChanged(handler: (user: User) => void, error?: (err) => any, completed?: Unsubscribe): Unsubscribe {
4848
this.authStateChangedHandler = handler;
4949
if (error) this.authStateOnErrorHandler = error;
5050
console.log(">> added onAuthStateChanged handler");
51+
52+
return () => {
53+
this.authStateChangedHandler = undefined;
54+
this.authStateOnErrorHandler = undefined;
55+
};
5156
}
5257

5358
public signOut(): Promise<any> {

src/firebase.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ export interface FirebaseQueryResult {
583583
value: any;
584584
}
585585

586+
export type Unsubscribe = () => void;
587+
586588
export function transaction(path: string, transactionUpdate: (a: any) => any,
587589
onComplete?: (error: Error | null, committed: boolean, dataSnapshot: DataSnapshot) => any): Promise<any>;
588590

0 commit comments

Comments
 (0)