import { Injectable } from '@angular/core' ; import { ActivatedRouteSnapshot , CanActivate , RouterStateSnapshot , Router } from '@angular/router' ; import { UserService } from './user.service' ; @ Injectable ( ) export class AuthGuard implements CanActivate { constructor ( private us , private router ) { } canActivate ( route , state ) { if ( ! this . us . isAuth ( ) ) { localStorage . setItem ( 'error' , 'You need to be logged in to access this page' ) ; this . router . navigate ( [ '/' ] ) ; return false ; } return true ; } }