Skip to content

Commit 31045a9

Browse files
authored
feat(auth): Adding user and idTokenResult Observables (angular#1642)
1 parent a6af604 commit 31045a9

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

docs/auth/getting-started.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 5. Getting started with Firebase Authentication
22

3-
`AngularFireAuth.authState` provides you an `Observable<firebase.User>` to monitor your application's authentication State.
3+
`AngularFireAuth.user` provides you an `Observable<User|null>` to monitor your application's authentication State.
44

55
`AngularFireAuth.auth` returns an initialized
66
`firebase.auth.Auth` instance, allowing you to log users in, out, etc. [See
@@ -16,7 +16,7 @@ import { firebase } from '@firebase/app';
1616
@Component({
1717
selector: 'app-root',
1818
template: `
19-
<div *ngIf="afAuth.authState | async as user; else showLogin">
19+
<div *ngIf="afAuth.user | async as user; else showLogin">
2020
<h1>Hello {{ user.displayName }}!</h1>
2121
<button (click)="logout()">Logout</button>
2222
</div>

src/auth/auth.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FirebaseAuth, User } from '@firebase/auth-types';
1+
import { FirebaseAuth, User, IdTokenResult } from '@firebase/auth-types';
22
import { FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';
33
import { Injectable, Inject, Optional, NgZone, PLATFORM_ID } from '@angular/core';
44
import { Observable, of, from } from 'rxjs';
@@ -16,15 +16,27 @@ export class AngularFireAuth {
1616
public readonly auth: FirebaseAuth;
1717

1818
/**
19-
* Observable of authentication state; as of 4.0 this is only triggered via sign-in/out
19+
* Observable of authentication state; as of Firebase 4.0 this is only triggered via sign-in/out
2020
*/
2121
public readonly authState: Observable<User|null>;
2222

2323
/**
24-
* Observable of the signed-in user's ID token; which includes sign-in, sign-out, and token refresh events
24+
* Observable of the currently signed-in user's JWT token used to identify the user to a Firebase service (or null).
2525
*/
2626
public readonly idToken: Observable<string|null>;
2727

28+
/**
29+
* Observable of the currently signed-in user (or null).
30+
*/
31+
public readonly user: Observable<User|null>;
32+
33+
/**
34+
* Observable of the currently signed-in user's IdTokenResult object which contains the ID token JWT string and other
35+
* helper properties for getting different data associated with the token as well as all the decoded payload claims
36+
* (or null).
37+
*/
38+
public readonly idTokenResult: Observable<IdTokenResult|null>;
39+
2840
constructor(
2941
@Inject(FirebaseOptionsToken) options:FirebaseOptions,
3042
@Optional() @Inject(FirebaseAppConfigToken) config:FirebaseAppConfig,
@@ -47,16 +59,22 @@ export class AngularFireAuth {
4759
)
4860
);
4961

50-
this.idToken = scheduler.keepUnstableUntilFirst(
62+
this.user = scheduler.keepUnstableUntilFirst(
5163
scheduler.runOutsideAngular(
5264
new Observable(subscriber => {
5365
const unsubscribe = this.auth.onIdTokenChanged(subscriber);
5466
return { unsubscribe };
5567
})
5668
)
57-
).pipe(switchMap((user:User) => {
69+
);
70+
71+
this.idToken = this.user.pipe(switchMap(user => {
5872
return user ? from(user.getIdToken()) : of(null)
59-
}));
73+
}));
74+
75+
this.idTokenResult = this.user.pipe(switchMap(user => {
76+
return user ? from(user.getIdTokenResult()) : of(null)
77+
}));
6078
}
6179

6280
}

0 commit comments

Comments
 (0)