Skip to content

Commit

Permalink
feat(auth): Adding user and idTokenResult Observables (#1642)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels authored May 14, 2018
1 parent a6af604 commit 31045a9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/auth/getting-started.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 5. Getting started with Firebase Authentication

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

`AngularFireAuth.auth` returns an initialized
`firebase.auth.Auth` instance, allowing you to log users in, out, etc. [See
Expand All @@ -16,7 +16,7 @@ import { firebase } from '@firebase/app';
@Component({
selector: 'app-root',
template: `
<div *ngIf="afAuth.authState | async as user; else showLogin">
<div *ngIf="afAuth.user | async as user; else showLogin">
<h1>Hello {{ user.displayName }}!</h1>
<button (click)="logout()">Logout</button>
</div>
Expand Down
30 changes: 24 additions & 6 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FirebaseAuth, User } from '@firebase/auth-types';
import { FirebaseAuth, User, IdTokenResult } from '@firebase/auth-types';
import { FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';
import { Injectable, Inject, Optional, NgZone, PLATFORM_ID } from '@angular/core';
import { Observable, of, from } from 'rxjs';
Expand All @@ -16,15 +16,27 @@ export class AngularFireAuth {
public readonly auth: FirebaseAuth;

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

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

/**
* Observable of the currently signed-in user (or null).
*/
public readonly user: Observable<User|null>;

/**
* Observable of the currently signed-in user's IdTokenResult object which contains the ID token JWT string and other
* helper properties for getting different data associated with the token as well as all the decoded payload claims
* (or null).
*/
public readonly idTokenResult: Observable<IdTokenResult|null>;

constructor(
@Inject(FirebaseOptionsToken) options:FirebaseOptions,
@Optional() @Inject(FirebaseAppConfigToken) config:FirebaseAppConfig,
Expand All @@ -47,16 +59,22 @@ export class AngularFireAuth {
)
);

this.idToken = scheduler.keepUnstableUntilFirst(
this.user = scheduler.keepUnstableUntilFirst(
scheduler.runOutsideAngular(
new Observable(subscriber => {
const unsubscribe = this.auth.onIdTokenChanged(subscriber);
return { unsubscribe };
})
)
).pipe(switchMap((user:User) => {
);

this.idToken = this.user.pipe(switchMap(user => {
return user ? from(user.getIdToken()) : of(null)
}));
}));

this.idTokenResult = this.user.pipe(switchMap(user => {
return user ? from(user.getIdTokenResult()) : of(null)
}));
}

}

0 comments on commit 31045a9

Please sign in to comment.