import { Injectable } from '@angular/core' ; import { Router , CanActivate , ActivatedRouteSnapshot , RouterStateSnapshot } from '@angular/router' ; import { UserService } from './user.service' ; @ Injectable ( ) export class AuthGuard implements CanActivate { constructor ( private router , private userService ) { } canActivate ( route , state ) { const user = this . userService . getUser ( ) ; if ( ! user && ! this . userService . isLoading ) { this . router . navigate ( [ '/login' ] , { queryParams : { returnUrl : state . url } } ) ; return false ; } return true ; } }