NuxtN
Nuxt2y ago
4 replies
ægteemil

Route guards for user permissions

Hey. I want to add navigation guards that check if a user has a required permissions before nagivation to a given page, or any sub-pages of it. Right now I have a somewhat crude implementation, and I'm curious if there's a better way of handling it.

if (to.fullPath.startsWith('/customers') && currentUserPermissions?.includes(Permission.CUSTOMER_READ))
  return true;

if (to.fullPath.startsWith('/sales') && currentUserPermissions?.includes(Permission.SALES_READ))
  return true;

// .. more 

return abortNavigation(
  createError({
    statusCode: 403,
    message: 'You do not have permissions to access this page.',
  }),
);
Was this page helpful?