Skip to content

Commit

Permalink
fix(auth): Clean up the authentication module
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels authored and davideast committed Oct 4, 2017
1 parent bb43189 commit 8ab3803
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 40 deletions.
61 changes: 21 additions & 40 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,47 @@ 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 {

/**
* 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<firebase.User>;
public readonly authState: Observable<firebase.User|null>;

/**
* Observable of the signed-in user's ID token; which includes sign-in, sign-out, and token refresh events
*/
idToken: Observable<firebase.User>;
public readonly idToken: Observable<string|null>;

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<firebase.User> {
const authState = Observable.create((observer: Observer<firebase.User>) => {
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<firebase.User|null>(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<firebase.User> {
const idToken = Observable.create((observer: Observer<firebase.User>) => {
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));
}
}
3 changes: 3 additions & 0 deletions tools/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 8ab3803

Please sign in to comment.