import { Injectable } from "@angular/core" ; import { CanActivate , Router } from "@angular/router" ; import { FirebaseAuthState } from "angularfire2" ; import { Observable } from "rxjs/Observable" ; import "rxjs/add/operator/do" ; import "rxjs/add/operator/map" ; import "rxjs/add/operator/take" ; import "rxjs/add/operator/first" ; import { AuthService } from "./auth.service" ; @ Injectable ( ) export class AuthGuard implements CanActivate { constructor ( private auth , private router ) { } canActivate ( ) { return this . auth . auth$ . map ( ( authState ) => { if ( authState ) { return true ; } else { this . router . navigateByUrl ( "/login" ) ; return false ; } } ) . first ( ) ; } }