Skip to content

feat(auth): Adding user and idTokenResult Observables #1642

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 4 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)
}));
}

}