src/app/providers/guards/composite-route-guard.ts
Methods |
constructor(router: Router, injector: Injector)
|
canActivate | |||||||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
|
|||||||||
Parameters :
Returns :
Observable<boolean>
|
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import {Injectable, Injector} from '@angular/core';
import { Observable } from "rxjs";
// code from https://stackoverflow.com/questions/45618687/angular-2-router-guards-order
@Injectable()
export class CompositeRouteGuard implements CanActivate {
constructor( protected router: Router,
protected injector: Injector ) {
}
canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<boolean> {
var compositeCanActivateObservable: Observable<boolean> = Observable.of(true);
let routeGuards = route.data.routeGuards;
if(routeGuards){
for( var i = 0; i < routeGuards.length; i++ ){
let routeGuard = this.injector.get(routeGuards[i]);
let canActivateObservable = routeGuard.canActivate(route, state);
compositeCanActivateObservable = compositeCanActivateObservable.flatMap( (bool) => {
if(!bool){
return Observable.of(false);
}
else{
return canActivateObservable;
}
});
}
}
return compositeCanActivateObservable;
}
}