import { Injectable } from '@angular/core' ; import { ActivatedRouteSnapshot , CanActivate , CanActivateChild , CanLoad , Route , Router , RouterStateSnapshot , } from '@angular/router' ; import { AuthService } from './auth.service' ; @ Injectable ( ) export class AuthGuard implements CanActivate , CanActivateChild , CanLoad { constructor ( private authService ) { } public canActivate ( route , state ) { const url = state . url ; return this . checkLogin ( url ) ; } public canActivateChild ( route , state ) { return this . canActivate ( route , state ) ; } public canLoad ( route ) { const url = `/${ route . path }` ; return this . checkLogin ( url ) ; } public checkLogin ( url ) { if ( this . authService . isLogged ) { return true ; } else { console . log ( url ) ; this . authService . redirectUrl = url ; location . href = 'login' ; return false ; } } }