-1
I have the following function:
const role = tokenPayload.params.role.map(r => {
return r.value;
}) [0];
She returns this:
author
If I remove index [0], 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 this 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 }
]
}
At the moment, I am 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.
Question answered here: How to Transform the value of an array?
– João Marcos Duarte