0
votes

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

1

1 Answers

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

Using route: ActivatedRouteSnapshot can solve you problem: