import { Injectable } from '@angular/core' ; import { Router , CanActivate , ActivatedRouteSnapshot , RouterStateSnapshot } from '@angular/router' ; import { Observable } from 'rxjs/Observable' ; import { AuthService } from './auth.service' ; import { AngularFireAuth } from 'angularfire2/auth' ; import 'rxjs/add/operator/do' ; import 'rxjs/add/operator/map' ; import 'rxjs/add/operator/take' ; @ Injectable ( ) export class AuthGuard implements CanActivate { constructor ( private afAuth , private router ) { } canActivate ( next , state ) { return this . afAuth . authState . take ( 1 ) . map ( user => ! ! user ) . do ( loggedIn => { if ( ! loggedIn ) { console . log ( 'access denied' ) ; this . router . navigate ( [ '/login' ] ) ; } } ) ; } }