import { Injectable } from '@angular/core' ; import { Router , CanActivate } from '@angular/router' ; import { AngularFire , FirebaseAuthState } from 'angularfire2' ; import { Observable } from 'rxjs/Observable' ; import 'rxjs/add/operator/do' ; import 'rxjs/add/operator/map' ; import 'rxjs/add/operator/take' ; /**
 * This class implements a guard for routes that require successful authentication.
 */ @ Injectable ( ) export class AuthenticationGuard implements CanActivate { param { AngularFire } angularFire param { Router } router constructor ( private angularFire , private router ) { } returns { Observable < boolean > } canActivate ( ) { return this . angularFire . auth . take ( 1 ) . map ( ( authState ) => { ! ! authState ? localStorage . setItem ( 'uid' , authState . uid ) : localStorage . removeItem ( 'uid' ) ; return ! ! authState ; } ) . do ( authenticated => { if ( ! authenticated ) { this . router . navigate ( [ '/login' ] ) ; } } ) ; } }