src/app/_guards/role.guard.ts
Methods |
constructor(router: Router)
|
||||||
Defined in src/app/_guards/role.guard.ts:8
|
||||||
Parameters :
|
canActivate | |||||||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
|
|||||||||
Defined in src/app/_guards/role.guard.ts:12
|
|||||||||
Parameters :
Returns :
Observable | Promise | boolean | UrlTree
|
import { Injectable } from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router} from '@angular/router';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class RoleGuard implements CanActivate {
constructor(private router: Router) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
const currentUser = JSON.parse(localStorage.getItem(atob('CICADA_USER')));
if (currentUser) {
if (route.data.roles && route.data.roles.indexOf(currentUser.role) === -1) {
this.router.navigate(['/']);
return false;
}
return true;
}
this.router.navigate(['/auth'], { queryParams: { returnUrl: state.url }});
return false;
}
}