import { Injectable } from '@angular/core' ; import { CanActivate , ActivatedRouteSnapshot , RouterStateSnapshot , Router } from '@angular/router' ; import { AuthService } from './auth.service' ; import { Observable } from 'rxjs/Observable' ; import { tap , map , take } from 'rxjs/operators' ; @ Injectable ( ) export class AuthGuard implements CanActivate { constructor ( private auth , private router ) { } canActivate ( next , state ) { return this . auth . user . pipe ( take ( 1 ) , map ( user => ! ! user ) , tap ( loggedIn => { if ( ! loggedIn ) { alert ( 'You must login first' ) ; this . router . navigate ( [ '/' ] ) ; } } ) ) ; } }