Depends on your architectur 🙂 But your way is easy & understandable to everyone whos checking your code.
If you want to abstract that part in the middleware itself and e.g. want to place the required permissions directly into page components, you could do something like:
<script setup lang="ts">
definePageMeta({
acl: ['user:view']
})
</script>
<template>
<div>
</div>
</template>
While your middleware (e.g. acl.global.ts) could look like:
export default defineNuxtRouteMiddleware((to, from) => {
const userPermissions = ['user:edit', 'user:view']
const requiredPermissions = to.meta.acl
if (!requiredPermissions || requiredPermissions.length === 0) {
return
}
const granted = requiredPermissions.every((permission) => userPermissions.includes(permission))
if (!granted) {
return abortNavigation({
statusCode: 403,
statusMessage: 'Forbidden',
})
}
})
Disadvantage of that way is, that you do not have permissions based on the url path and needs to repeat yourself on subpages of e.g. /customers and /customers/{id} in the definePageMeta
To solve that you could work with NestedPages.
But keep in mind: with more layers and abstraction, its more complicated to understand for other developers how your code and permissions works, which permission is required to visit pages.