I have the following function:
const role = tokenPayload.params.role.map(r => {
return r.value;
}) [0];
It returns this:
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 example? The "role" const will be used in a comparison that accepts only the result in that particular format.
I'll 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 }
]
}
At the moment, I'm only passing one function per user. However, I would like to leave the dynamic code in case any user has more than one function in the future.