import { Injectable } from '@angular/core' ; import { CanActivate , ActivatedRouteSnapshot , RouterStateSnapshot , UrlTree , Router } from '@angular/router' ; import { Observable } from 'rxjs' ; import { AuthService } from '../services/auth.service' ; import { map } from 'rxjs/operators' ; @ Injectable ( { providedIn : 'root' } ) export class AuthGuard implements CanActivate { constructor ( private router , private authService ) { } canActivate ( next , state ) { return this . authService . appUser$ . pipe ( map ( user => { if ( user ) { return true ; } this . router . navigate ( [ '/' ] , { queryParams : { returnUrl : state . url } } ) ; return false ; } ) ) ; } }