From 069a4808222f815dcfb5dc1eb014895e5170a8ef Mon Sep 17 00:00:00 2001 From: James Daniels Date: Sun, 13 May 2018 17:25:29 -0700 Subject: [PATCH 1/2] feat(auth): Adding user and idTokenResult Observables --- docs/auth/getting-started.md | 4 ++-- src/auth/auth.ts | 30 ++++++++++++++++++++++++------ yarn.lock | 27 +++++++-------------------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/docs/auth/getting-started.md b/docs/auth/getting-started.md index 275d2e526..6c56088de 100644 --- a/docs/auth/getting-started.md +++ b/docs/auth/getting-started.md @@ -1,6 +1,6 @@ # 5. Getting started with Firebase Authentication -`AngularFireAuth.authState` provides you an `Observable` to monitor your application's authentication State. +`AngularFireAuth.user` provides you an `Observable` 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 @@ -16,7 +16,7 @@ import * as firebase from 'firebase/app'; @Component({ selector: 'app-root', template: ` -
+

Hello {{ user.displayName }}!

diff --git a/src/auth/auth.ts b/src/auth/auth.ts index 092bfcfe1..897ce6fa9 100644 --- a/src/auth/auth.ts +++ b/src/auth/auth.ts @@ -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 } from 'rxjs'; @@ -18,15 +18,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; /** - * 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; + /** + * Observable of the currently signed-in user (or null). + */ + public readonly user: Observable; + + /** + * 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; + constructor( @Inject(FirebaseOptionsToken) options:FirebaseOptions, @Optional() @Inject(FirebaseAppConfigToken) config:FirebaseAppConfig, @@ -49,16 +61,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) + })); } } diff --git a/yarn.lock b/yarn.lock index f93784576..dfd43c809 100644 --- a/yarn.lock +++ b/yarn.lock @@ -62,6 +62,7 @@ dom-storage "2.1.0" tslib "1.9.0" xmlhttprequest "1.8.0" + "@firebase/auth-types@0.3.2", "@firebase/auth-types@^0.3.2": version "0.3.2" resolved "https://registry.yarnpkg.com/@firebase/auth-types/-/auth-types-0.3.2.tgz#fc636084eb82cb098e4e76efc5fffd315e22abdd" @@ -79,27 +80,14 @@ "@firebase/database@0.3.1", "@firebase/database@^0.3.1": version "0.3.1" resolved "https://registry.yarnpkg.com/@firebase/database/-/database-0.3.1.tgz#1ca0797b8e9a243d383d31b1873373b84a2048b9" - -"@firebase/auth-types@0.2.1", "@firebase/auth-types@^0.2.1": - version "0.2.1" - resolved "https://registry.yarnpkg.com/@firebase/auth-types/-/auth-types-0.2.1.tgz#83a03939358ce8a59de85404b9ed7ca13a51548f" - -"@firebase/auth@0.4.2", "@firebase/auth@^0.4.2": - version "0.4.2" - resolved "https://registry.yarnpkg.com/@firebase/auth/-/auth-0.4.2.tgz#63044e6ca6fb98ddc2de5a442f56f9d612ed1903" dependencies: - "@firebase/auth-types" "0.2.1" - -"@firebase/database-types@0.2.1", "@firebase/database-types@^0.2.1": - version "0.2.1" - resolved "https://registry.yarnpkg.com/@firebase/database-types/-/database-types-0.2.1.tgz#f83a6d03af5f8c93276ceb89e1f31e4664c9df1b" - -"@firebase/database@0.2.2", "@firebase/database@^0.2.2": - version "0.2.2" - resolved "https://registry.yarnpkg.com/@firebase/database/-/database-0.2.2.tgz#a8a0709644d7f281b400e983c71c8c65fba90c70" + "@firebase/database-types" "0.3.1" + "@firebase/logger" "0.1.1" + "@firebase/util" "0.2.0" + faye-websocket "0.11.1" + tslib "1.9.0" "@firebase/firestore-types@0.4.1", "@firebase/firestore-types@^0.4.1": - version "0.4.1" resolved "https://registry.yarnpkg.com/@firebase/firestore-types/-/firestore-types-0.4.1.tgz#9a96dff26f0537b13ff119b5e7ab4bddad2068d1" @@ -112,7 +100,7 @@ "@firebase/webchannel-wrapper" "0.2.8" grpc "1.10.1" tslib "1.9.0" - + "@firebase/functions-types@0.1.2": version "0.1.2" resolved "https://registry.yarnpkg.com/@firebase/functions-types/-/functions-types-0.1.2.tgz#f291b2945218a97d9ab0e20f2fdec2cef6f90f94" @@ -1918,7 +1906,6 @@ fined@^1.0.1: object.pick "^1.2.0" parse-filepath "^1.0.1" - firebase@^5.0.2: version "5.0.2" resolved "https://registry.yarnpkg.com/firebase/-/firebase-5.0.2.tgz#5c0d647acc10552d15169e5f8482413aff1264cb" From d1576c5bfb61e386a3f0a22c7b197376531aa234 Mon Sep 17 00:00:00 2001 From: James Daniels Date: Mon, 14 May 2018 09:48:17 -0700 Subject: [PATCH 2/2] Update getting-started.md --- docs/auth/getting-started.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/auth/getting-started.md b/docs/auth/getting-started.md index 6c56088de..42556f237 100644 --- a/docs/auth/getting-started.md +++ b/docs/auth/getting-started.md @@ -1,6 +1,6 @@ # 5. Getting started with Firebase Authentication -`AngularFireAuth.user` provides you an `Observable` to monitor your application's authentication State. +`AngularFireAuth.user` provides you an `Observable` 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