import { useAuthStore } from '~/stores/auth' /** * Garde-fou global : empêche les utilisateurs non-admin d'accéder aux pages * sous /admin/*. Renvoie vers la home pour les utilisateurs authentifiés * non-admin, et vers /login pour les non authentifiés. * * L'API back rejette de toute façon les actions admin avec un 403, mais ce * middleware évite l'affichage des pages vides / en erreur quand un user * tape directement l'URL /admin/... */ export default defineNuxtRouteMiddleware(async (to) => { if (!to.path.startsWith('/admin')) { return } const auth = useAuthStore() await auth.ensureSession() if (!auth.isAuthenticated) { return navigateTo('/login') } if (!auth.isAdmin) { return navigateTo('/') } })