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