Send data to guardians in the angle

0

I need to make a guard to check if a user has permission to the system screens.

I have an array stored in the localstorage that tells me the permissions of a user.

Example: [1,2,5], where the number is the screen id that the person can access.

I would like to know if I have been sending data from my route file to the guardian.

Something like:

{ path: 'custofixo', component: CustofixoComponent, data:{idtela: 1}, canActivate:[PermissoesTelaGuard], pathMatch: 'full' }

How can I recover this idtela from my guardian?

    
asked by anonymous 30.08.2018 / 17:45

1 answer

0

You can use a service that makes the toke or permissions explicitly available.

user.service.ts

...
getPermissions(){
   const currentUser = localStorage.getItem(currentUser);
   return currentUser.permissions; // Aqui você retorna o array com suas permissões
}
...

app.guard.ts

 constructor(private userService: UserService) { }

 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    const idTela = route.snapshot.data['idtela'];
    const permissions = this.userService.getPermissions();
    if (!permissions.includes(idTela)) {
      alert('Activation blocked');
      return false;
    }
    return true;
  }

I believe this will solve.

Good luck!

    
31.08.2018 / 20:24