-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathadal.guard.ts
32 lines (25 loc) · 1011 Bytes
/
adal.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
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { AdalService } from './adal.service';
@Injectable()
export class AdalGuard implements CanActivate, CanActivateChild {
constructor(private adalService: AdalService) { }
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
const expectedRole = route.data.expectedRole;
if(expectedRole){
return (this.adalService.userInfo.authenticated && this.adalService.userInfo.profile.roles.includes(expectedRole));
} else {
return this.adalService.userInfo.authenticated;
}
}
public canActivateChild(
childRoute: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
return this.canActivate(childRoute, state);
}
}