export default defineNuxtRouteMiddleware(async (to, from) => { // Skip auth check for public routes const publicRoutes = ['/login', '/logout']; if (publicRoutes.includes(to.path)) { return; } if (process.client) { try { // Validate authentication with new endpoint const { data: validateUser } = await useFetch("/api/auth/validate", { method: "GET", server: false // Client-side only }); // If user is not logged in, redirect to login page if (validateUser.value && validateUser.value.statusCode === 401) { const { $swal } = useNuxtApp(); if ($swal) { await $swal.fire({ title: "Authentication Required", text: "Please log in to access this page.", icon: "warning", confirmButtonText: "Login", allowOutsideClick: false }); } return navigateTo('/login'); } } catch (error) { console.error('Auth middleware error:', error); return navigateTo('/login'); } } });