I've been looking to use Nuxt middleware in layout. But I'm not sure if it is possible, but, since I used it inNuxt 2, it might be possible inNuxt 3.
The project has 2 different layouts:Public.vue
andAdmin.vue
. I only want to use the middleware in pages usingManage Layout. Because the page using it can only be accessed by logged in users and the check is done inside the middleware.
I tried this (doesn't work):
Manage layout|Manage.vue
Middleware | adminAuth.ts
export default defineNuxtRouteMiddleware((to, from) => { console.log(to); console.log("Acessando o admin auth middleware"); })
Middleware cannot be used in layout because middleware can only be used in pages, but you can try this method.
Create global middleware by declaring the
.global
suffix after the middleware file name, such asauth.global.ts
.In the
auth.global.ts
file, you can use layout metas as logic to simulate as if the middleware was in your layout settings.The example logic is like this
Hope this helps
you can not. Middleware only works on pages.
Instead, create a parent Page component using the auth middleware and the
NuxtPage
component inside the template. For more information onNested Routing, see the Nuxt 3 documentation.Example:
/pages/admin.vue(Route =>/admin)
/pages/admin(folder)
admin/order.vue routing =>/admin/orders
admin/page.vue route =>/admin/some-route