1

I have the following function:

const role: string = tokenPayload.params.role.map(r => {
  return r.value;
}) [0];

It returns the following result:

author

If I remove the index [0], it returns this:

["author", "admin"]

Is it possible for the function to return all values in the same format as the first exemple?

The const "role" will be used in a comparison === that only accepts result in that specific format. I will put the full function for better understanding:

canActivateChild(route: ActivatedRouteSnapshot): boolean {
    const helper = new JwtHelperService();
    const expectedRole = route.data.expectedRole;
    const token = this.authService.getToken();
    const tokenPayload = helper.decodeToken(token);
    const role: string = tokenPayload.params.role.map(r => {
  return r.value;
}) [0];
console.log(role);
if (!this.authService.isAuthenticated() || role !== expectedRole) {
  this.router.navigate(['/admin']);
  return false;
}
return true;
}

router component:

{
    path: 'container-users',
    component: ContainerUsersComponent,
    canActivateChild: [AuthGuard],
    data: {
      expectedRole: 'admin'
    },
    children: [
      { path: '', component: ListUsersComponent },
      { path: 'list-users', component: ListUsersComponent },
      { path: 'form-new-user', component: FormNewUserComponent }
    ]
  }
Benjamin Scholtz
  • 823
  • 6
  • 15
  • Can you provide an example of the format you would like to see? "author, admin"? – Joshua Kemmerer Aug 20 '18 at 19:55
  • The result of the map function is an array. When you provide the index it is selecting an element in that array as a string. The first result is simply the first element of the array - the elements are all of the same type/format, which is `string`. It is not possible to use `===` on multiple elements, unless as @JoshuaKemmerer mentioned you combine the elements to create a single string. – Benjamin Scholtz Aug 20 '18 at 20:00
  • Tks, guys. I edited my question for more informations. At the moment, I'm only passing one function per user. However, I would like to leave the dynamic code, in case some user has more than one function. – João Marcos Duarte Aug 20 '18 at 20:18

2 Answers2

4

It seems the user in your application can have multiple roles and what you are trying to do is checking whether any of these roles attached to the user is expectedRole. expectedRole is a single string value, while roles attached to the user are represented by the array of some objects, where the role name is stored in value property, so you cannot use === operator, you should use a method like indexOf(), some() or includes() to verify user has expectedRole assigned.

So, instead of selecting all role names with map function, I would directly check the required role:

const hasExpectedRole = tokenPayload.params.role.some(r => r.value === expectedRole)

and later, in if statement

if (... || !hasExpectedRole)
tdragon
  • 3,209
  • 1
  • 16
  • 17
0

Instead of taking the first element of the array generated by map, use the string array and check if it contains the expectedRole string:

roles = tokenPayload.params.role.map(r => {
  return r.value;
});

// Use the following function
(roles.indexOf(expectedRole) == -1);

// Instead of this
role !== expectedRole;

See How to find if an array contains a specific string in JavaScript? - this explains how to check if an array of values contains a single value.

Benjamin Scholtz
  • 823
  • 6
  • 15