import { Injectable } from '@angular/core' ; import { CanActivate , ActivatedRouteSnapshot , RouterStateSnapshot , UrlTree , Router , CanActivateChild } from '@angular/router' ; import { Observable } from 'rxjs' ; import { AuthService } from '../services/auth.service' ; @ Injectable ( { providedIn : 'root' } ) export class AuthGuard implements CanActivate , CanActivateChild { constructor ( private router , private authenticationService ) { } public canActivate ( next , state ) { const currentUser = this . authenticationService . currentUserValue ; const currentUserToken = this . authenticationService . currentUserTokenValue ; if ( currentUser && currentUserToken ) { return true ; } console . log ( 'NO USER -> REDIRECT TO LOGIN' ) ; this . authenticationService . logout ( ) ; return false ; } public canActivateChild ( childRoute , state ) { const currentUser = this . authenticationService . currentUserValue ; const currentUserToken = this . authenticationService . currentUserTokenValue ; if ( currentUser && currentUserToken ) { return true ; } console . log ( 'NO USER -> REDIRECT TO LOGIN' ) ; this . authenticationService . logout ( ) ; this . router . navigate ( [ 'auth' ] ) ; return false ; } }