import { Injectable } from '@angular/core' ; import { CanActivate , Router , ActivatedRouteSnapshot , RouterStateSnapshot } from '@angular/router' ; import { Observable } from 'rxjs' ; import { AngularFireAuth } from '@angular/fire/auth' ; import { take , map , tap } from 'rxjs/operators' ; @ Injectable ( { providedIn : 'root' } ) export class AuthguardService implements CanActivate { constructor ( private afAuth , private router ) { } canActivate ( route , state ) { return this . afAuth . authState . pipe ( take ( 1 ) , map ( user => ! ! user ) , tap ( loggedIn => { if ( ! loggedIn ) { this . router . navigate ( [ '/login' ] ) ; } } ) ) ; } }