-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.guard.ts
37 lines (31 loc) · 1.39 KB
/
role.guard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Injectable } from '@angular/core';
import { CanActivate, CanLoad, Route, UrlSegment, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { AuthManagerService } from '../services/auth-manager.service';
import { AuthGuard } from './auth.guard';
@Injectable({
providedIn: 'root'
})
export class RoleGuard implements CanActivate, CanLoad {
constructor(
private authManagerService: AuthManagerService,
private authGuard: AuthGuard,
private router: Router,
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
: Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.isAuthorized();
}
canLoad(route: Route, segments: UrlSegment[])
: Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.isAuthorized();
}
public isAuthorized(): Observable<boolean | UrlTree> {
return this.authGuard.isAuthenticated()
.pipe(
switchMap(isAuthenticated => isAuthenticated === true ? this.authManagerService.isAuthorized() : of(isAuthenticated)),
map(isAuthorized => isAuthorized === false ? this.router.parseUrl('/error/403') : isAuthorized),
);
}
}