Skip to content

Instantly share code, notes, and snippets.

@nhh
Last active May 29, 2018 09:37
Show Gist options
  • Save nhh/ae3acb04cda5c9f29277dd7a57880c46 to your computer and use it in GitHub Desktop.
Save nhh/ae3acb04cda5c9f29277dd7a57880c46 to your computer and use it in GitHub Desktop.
Authenticated Guard for nested views. (child views)
import { Injectable } from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild} from '@angular/router';
import {Observable} from 'rxjs';
import {AuthService} from '../service/auth/auth.service';
@Injectable({
providedIn: 'root'
})
export class IsAuthenticatedGuard implements CanActivate, CanActivateChild {
constructor(
private authService: AuthService
) {};
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.authService.isAuthenticated();
}
canActivateChild(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.authService.isAuthenticated();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment