import { Injectable } from '@angular/core' ; import { CanActivate , Router } from '@angular/router' ; import { AuthService } from './auth.service' ; @ Injectable ( ) export class EnsureAuthenticated implements CanActivate { constructor ( private auth , private router ) { } canActivate ( ) { if ( localStorage . getItem ( 'token' ) ) { return true ; } else { this . router . navigateByUrl ( '/login' ) ; return false ; } } }