Guest User

Untitled

a guest
Mar 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. ngOnInit() {
  2. this.filteredRoutes$ = from(this.routes).pipe(
  3. concatMap((route) => {
  4. // handle if nothing is in canActivate
  5. if (!route.canActivate || !route.canActivate.length) {
  6. // create an object that has the route and result for filtering
  7. return of({ route, result: true });
  8. }
  9.  
  10. return from(route.canActivate).pipe(
  11. // execute the guard
  12. switchMap(guard => {
  13. const instance = this.injector.get(guard);
  14. const result = instance.canActivate();
  15. if (result instanceof Observable) {
  16. return result;
  17. } else {
  18. return of(result);
  19. }
  20. }
  21. ),
  22. // aggregate the guard results for the route
  23. toArray(),
  24. // ensure all results are true
  25. map(results => results.every(r => r)),
  26. // create an object that has the route and result for filtering
  27. map(result => ({ route, result })));
  28. }),
  29. // filter out the invalid guards
  30. filter(routeCanActivateResult => routeCanActivateResult.result),
  31. // return just the route
  32. map(routeCanActivateResult => routeCanActivateResult.route),
  33. // turn it back into an array
  34. toArray()
  35. );
  36. }
Add Comment
Please, Sign In to add comment