import { Injectable } from '@angular/core' ; import { CanActivate , Router , ActivatedRouteSnapshot , RouterStateSnapshot , CanActivateChild } from '@angular/router' ; import { UsersService } from "./users.service" ; @ Injectable ( ) export class AuthGuard implements CanActivate , CanActivateChild { constructor ( private userService , private router ) { } checkLogin ( url ) { if ( this . userService . isLoggedIn ( ) ) { return true ; } this . userService . redirectUrl = url ; this . router . navigate ( [ '/login' ] ) ; return false ; } canActivate ( route , state ) { let url = state . url ; return this . checkLogin ( url ) ; } canActivateChild ( route , state ) { return this . canActivate ( route , state ) ; } }