5.2: Adding Router Guards
Adding Guards
ng g guard guards/auth --no-spec@Injectable()
export class AuthGuard implements CanActivate {
constructor(
private _authService: AuthService,
private _router: Router) {}
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
if (this._authService.loggedIn()) {
return true;
}
console.log('You need to be logged in to access that.');
this._router.navigate(['/home']);
return false;
}
}Add the AuthGuard as a service
Adding AuthGuard to the Routes
Last updated