0

I am unable to access the neededPermissions array in the routes. I clearly have the array neededPermissions in routes but every time I try to access the array, console logs undefined.

How can I get the neededPermissions array from the meta object in routes? Clearly to.meta.neededPermissions prints undefined, which make's it impossible to loop through currentUser.

{
    path: "/administration",
    name: "Administration",
    component: PageAdministration,
    meta: {
      requiresAuth: true,
      neededPermissions: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
  },
}

function to authorize users

  console.log(to.meta.neededPermissions );
if(currentUser!==null && to.meta.neededPermissions !== undefined)
  {
    for(let i = 0;i<currentUser.teams.length;i++){
      const team = currentUser.teams[i]
      const validPermissions = team.permissions.filter((item) => { return to.meta.neededPermissions.includes(item.permissionType); });
      const mappedValidPermissions = validPermissions.map((item) => { return item.permissionType;  });
      if (!to.meta.neededPermissions.every(i=>mappedValidPermissions.includes(i))) {
        isAllowed = false;
        next({ path: "/:notFound(.*)" });   
        break; 
      } 
    } if(isAllowed) next();
}
    
4
  • based on your other question, I see this function is called in router.beforeEach, so every route navigation is going to result in console.log(to.meta.neededPermissions), but as far as I can see only one route (/administration) provides meta.neededPermissions. My guess is that you have some other route initially loading trying to call this function which doesn't have the same meta properties provided. Commented Jan 11, 2023 at 14:54
  • You are absolutely right. That's why I am using if condition to check the neededPermissions. I would appreciate if you have any other solution for my problem. Thank you. Commented Jan 11, 2023 at 15:28
  • you've set up your routes so neededPermissions is only defined for the /administration route. meaning the if statement will only be true if currentUser exists and the current route is /administration. otherwise to.meta.neededPermissions being undefined is expected. are you trying to have the if statement be true for other situations? Commented Jan 11, 2023 at 15:36
  • Thank you. I resolved the issue by adding the else. But I am not sure how to hide the component using authorizeUser method in store. Commented Jan 12, 2023 at 8:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.