import { getCookie } from 'h3' export default defineNuxtRouteMiddleware(async (to, from) => { const protectedRoutes = ['/info/programs', '/dashboard', '/admin'] const needsAuth = protectedRoutes.some(route => to.path.startsWith(route)) if (!needsAuth) { return } if (import.meta.server) { const event = useRequestEvent() const sessionCookie = getCookie(event, 'session') // Replace with actual cookie name if (!sessionCookie || sessionCookie === 'null' || sessionCookie === '') { // Redirect to login endpoint with redirect target const redirectUrl = encodeURIComponent(`https://${event.node.req.headers.host}${to.fullPath}`) return navigateTo(`/api/auth/login?redirect=${redirectUrl}`, { external: true }) } return } if (import.meta.client) { try { const session = await $fetch('/api/auth/session') if (!session || session.user == null) { const redirectUrl = encodeURIComponent(window.location.origin + to.fullPath) return navigateTo(`/api/auth/login?redirect=${redirectUrl}`, { external: true }) } } catch { const redirectUrl = encodeURIComponent(window.location.origin + to.fullPath) return navigateTo(`/api/auth/login?redirect=${redirectUrl}`, { external: true }) } } })