added role.guard.ts.ts again
Signed-off-by: supphakitd <67319010028@technictrang.ac.th>
This commit is contained in:
27
ng-ttc-frontend/src/app/services/role.guard.ts.ts
Normal file
27
ng-ttc-frontend/src/app/services/role.guard.ts.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class RoleGuard {
|
||||
constructor(private router: Router) { }
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
|
||||
const denyRole = route.data?.['denyRole'];
|
||||
const usrrol = localStorage.getItem('usrrol');
|
||||
|
||||
if (denyRole && usrrol === denyRole) {
|
||||
// blocked: navigate away (adjust target as needed)
|
||||
this.router.navigate(['/']);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
|
||||
return this.canActivate(route, state);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user