Files
nlcc-itinerary/middleware/auth.ts

22 lines
558 B
TypeScript

export default defineNuxtRouteMiddleware(async (to, from) => {
const token = useCookie('auth_token')
if (!token.value && to.path === '/admin') {
// Redirect to login page if not authenticated and trying to access admin
return navigateTo('/login')
}
if (token.value) {
try {
// Verify token with server
await $fetch('/api/auth/verify')
} catch {
// If token is invalid, clear it and redirect to login
token.value = ''
if (to.path === '/admin') {
return navigateTo('/login')
}
}
}
})