I've been looking to use Nuxt middleware in a layout. But I am not sure if I even can, however, since I used it in Nuxt 2, it may be possible in Nuxt 3.
The project has 2 different layouts: Public.vue
and Admin.vue
. I only want to use the middleware in pages that consume the Admin layout. Because the pages that use it should be accessed only by logged-in users, and it will be checked inside the middleware.
I tried this (doesn't work):
Admin layout | Admin.vue
<template>
<div>
<client-only>
<admin-header />
</client-only>
<main>
<slot />
</main>
<client-only>
<admin-footer />
</client-only>
</div>
</template>
<script lang="ts">
import AdminHeader from "~~/components/admin/Header.vue"
import AdminFooter from "~~/components/admin/Footer.vue"
definePageMeta({
middleware: "admin-auth"
});
</script>
Middleware | adminAuth.ts
export default defineNuxtRouteMiddleware((to, from) => {
console.log(to);
console.log("Acessando o admin auth middleware");
})
middleware
directory? Maybe try to name itadmin-auth
, not sure if this may help. This seems to work pretty well: v3.nuxtjs.org/examples/routing/middleware But yeah, client side middleware is feasible. – Nitre