diff --git a/src/auth/auth.ts b/src/auth/auth.ts index 46147dba6..16fb1dcbb 100644 --- a/src/auth/auth.ts +++ b/src/auth/auth.ts @@ -2,11 +2,12 @@ import * as firebase from 'firebase/app'; import 'firebase/auth'; import { Injectable, NgZone } from '@angular/core'; import { Observable } from 'rxjs/Observable'; -import { Observer } from 'rxjs/Observer'; import { observeOn } from 'rxjs/operator/observeOn'; import { FirebaseApp, ZoneScheduler } from 'angularfire2'; -export type Auth = firebase.auth.Auth; +import 'rxjs/add/operator/switchMap'; +import 'rxjs/add/observable/of'; +import 'rxjs/add/observable/fromPromise'; @Injectable() export class AngularFireAuth { @@ -14,54 +15,34 @@ export class AngularFireAuth { /** * Firebase Auth instance */ - auth: firebase.auth.Auth; + public readonly auth: firebase.auth.Auth; /** * Observable of authentication state; as of 4.0 this is only triggered via sign-in/out */ - authState: Observable; + public readonly authState: Observable; /** * Observable of the signed-in user's ID token; which includes sign-in, sign-out, and token refresh events */ - idToken: Observable; + public readonly idToken: Observable; constructor(public app: FirebaseApp) { - this.authState = FirebaseAuthStateObservable(app); - this.idToken = FirebaseIdTokenObservable(app); this.auth = app.auth(); - } - -} -/** - * Create an Observable of Firebase authentication state. Each event is called - * within the current zone. - * @param app - Firebase App instance - */ -export function FirebaseAuthStateObservable(app: FirebaseApp): Observable { - const authState = Observable.create((observer: Observer) => { - app.auth().onAuthStateChanged( - (user: firebase.User) => observer.next(user!), - (error: firebase.auth.Error) => observer.error(error), - () => { observer.complete(); return undefined; } - ); - }); - return observeOn.call(authState, new ZoneScheduler(Zone.current)); -} + const authState$ = new Observable(subscriber => { + const unsubscribe = this.auth.onAuthStateChanged(subscriber); + return { unsubscribe }; + }); + this.authState = observeOn.call(authState$, new ZoneScheduler(Zone.current)); + + const idToken$ = new Observable(subscriber => { + const unsubscribe = this.auth.onIdTokenChanged(subscriber); + return { unsubscribe }; + }).switchMap(user => { + return user ? Observable.fromPromise(user.getIdToken()) : Observable.of(null) + }); + this.idToken = observeOn.call(idToken$, new ZoneScheduler(Zone.current)); + } -/** - * Create an Observable of Firebase ID token. Each event is called - * within the current zone. - * @param app - Firebase App instance - */ -export function FirebaseIdTokenObservable(app: FirebaseApp): Observable { - const idToken = Observable.create((observer: Observer) => { - app.auth().onIdTokenChanged( - (user: firebase.User) => observer.next(user!), - (error: firebase.auth.Error) => observer.error(error), - () => { observer.complete(); return undefined; } - ) - }); - return observeOn.call(idToken, new ZoneScheduler(Zone.current)); -} +} \ No newline at end of file diff --git a/tools/build.js b/tools/build.js index 6492a1c65..aa7d7ecbc 100644 --- a/tools/build.js +++ b/tools/build.js @@ -37,7 +37,10 @@ const GLOBALS = { 'rxjs/add/operator/skipUntil': 'Rx.Observable.prototype', 'rxjs/add/operator/skipWhile': 'Rx.Observable.prototype', 'rxjs/add/operator/withLatestFrom': 'Rx.Observable.prototype', + 'rxjs/add/operator/switchMap': 'Rx.Observable.prototype', 'rxjs/add/observable/merge': 'Rx.Observable', + 'rxjs/add/observable/of': 'Rx.Observable.prototype', + 'rxjs/add/observable/fromPromise': 'Rx.Observable.prototype', 'rxjs/add/operator/delay': 'Rx.Observable', 'rxjs/add/operator/debounce': 'Rx.Observable', 'rxjs/observable/fromEvent': 'Rx.Observable',