import { Injectable } from '@angular/core'; import { CanActivate, CanActivateChild, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; import { AuthService } from '../services'; @Injectable() export class UnauthGuard implements CanActivate{ constructor(protected router: Router, protected auth: AuthService){} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean{ /** * if user is logged in, * navigate to '/' */ if(this.auth.check()){ this.router.navigate(['/']); return false; } /** * if user is not logged in, * proceed, typically to a sign in form, */ return true; } canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean{ return this.canActivate(route, state); } }