I am trying to implement guards in my Angular application. So far I have 3 guards setup, but I would like to have them setup in an OR or ANY mode, where if any of the guards allow the request the route can be activated.
Does someone have a way of doing this?
My guards are quite simple with all three looking the same as follows:
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { AppSettings } from './AppSettings';
@Injectable({ providedIn: 'root' })
export class GuardianGuard implements CanActivate {
constructor(
private router: Router,
private http: HttpClient
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
//Get the users role from the API based on the JWT they were authenticated with
return this.http.get(`${AppSettings.API_ENDPOINT}/Role`).pipe(map(data => {
//Check if the returned server role is that of the guardian
if (data === "3") {
//Allow the action if it is
return true;
} else {
//Return to login and deny action if it is not
this.router.navigate(['/login']);
return false;
}
}));
}
}