import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, Subject, BehaviorSubject } from 'rxjs/Rx';
import { FirebaseAuth, FirebaseAuthState } from 'angularfire2';

@Injectable()
export class AuthService {

	auth$: BehaviorSubject<FirebaseAuthState> = new BehaviorSubject(null);

	constructor (private auth: FirebaseAuth, private router: Router) {
		this.auth.subscribe(
			data => {
				this.auth$.next(data);
			},
			err => {
				this.auth$.error(err);
			}
		);
	}

	login(email: string, password: string): Observable<FirebaseAuthState> {
		return this.fromFirebaseAuthPromise(this.auth.login({ email, password }));
	}

	register(email: string, password: string): Observable<FirebaseAuthState> {
		return this.fromFirebaseAuthPromise(this.auth.createUser({ email, password }));
	}

	fromFirebaseAuthPromise(promise): Observable<any> {

		const subject = new Subject<any>();

		promise
			.then(res => {
				subject.next(res);
				subject.complete();
			})
			.catch(err => {
				subject.error(err);
				subject.complete();
			});

			return subject.asObservable();
	}

	logout() {
		this.auth.logout();
		this.router.navigate(['/home']);
	}

}

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, Subject, BehaviorSubject } from 'rxjs/Rx';
import { FirebaseAuth, FirebaseAuthState } from 'angularfire2';

@Injectable()
export class AuthService {

	auth$: BehaviorSubject<FirebaseAuthState> = new BehaviorSubject(undefined);

	constructor (private auth: FirebaseAuth, private router: Router) {
		this.auth.subscribe(
			data => {
				this.auth$.next(data);
			},
			err => {
				this.auth$.error(err);
			}
		);
	}

	login(email: string, password: string): Observable<FirebaseAuthState> {
		return this.fromFirebaseAuthPromise(this.auth.login({ email, password }));
	}

	register(email: string, password: string): Observable<FirebaseAuthState> {
		return this.fromFirebaseAuthPromise(this.auth.createUser({ email, password }));
	}

	fromFirebaseAuthPromise(promise): Observable<any> {

		const subject = new Subject<any>();

		promise
			.then(res => {
				subject.next(res);
				subject.complete();
			})
			.catch(err => {
				subject.error(err);
				subject.complete();
			});

			return subject.asObservable();
	}

	logout() {
		this.auth.logout();
		this.router.navigate(['/home']);
	}

}

Change initial value of `auth$` behavior subject
Change initial value of `auth$` behavior subject

That way we can tell if Firebase is still fetching the auth state of
the user
