简体   繁体   中英

Get User Roles in Angular Application

I want to get User Roles in the angular application but in my case its dose not working code is bellow

export const appRoutes: Routes [{path:'home',component:HomeComponentcanActivate: [AuthGuard] }, { path: 'admin',component: AdminPanelComponent, canActivate: [AuthGuard], data: {roles: ['SuperAdmin']}},

i want to get Role for admin because other user will not access the admin component here is is my auth guard code

canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
if (sessionStorage.getItem('userToken') != null) {
  debugger
  let roles = next.data['roles'] as Array<string>;
  console.log(next.data['roles']);
  if (roles) {
    var match = this.userService.roleMatch(roles);
    if (match) {
      return true;
    } else {
      this.router.navigate(['/forbiden']);
    }
  }
} else {
  this.router.navigate(['/signin']);
  return false;
}}

its giving roles undefined not get in to IF condition

canActivate(route: ActivatedRouteSnapshot) {
  console.log(route.data);
}

Using route: ActivatedRouteSnapshot can solve you problem:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM