import { Injectable } from '@angular/core' ; import { CanActivate , Router } from '@angular/router' ; import { UserService } from './user.service' ; @ Injectable ( ) export class CanActivateGuard implements CanActivate { private connected = false ; constructor ( private router , private userService ) { this . userService . getCurrent ( ) . subscribe ( ( user ) => { this . connected = user . connected ; } ) ; } public canActivate ( ) { if ( ! this . connected ) { this . router . navigate ( [ 'login' ] ) ; } return this . connected ; } }