Files
harheimertc/middleware/auth.global.js
2025-10-21 14:29:52 +02:00

23 lines
527 B
JavaScript

export default defineNuxtRouteMiddleware(async (to, from) => {
// Only run on client-side
if (process.server) return
const authStore = useAuthStore()
// Check if route requires authentication
const requiresAuth = to.meta.middleware === 'auth'
if (requiresAuth) {
// Check auth status if not already checked
if (!authStore.isLoggedIn) {
await authStore.checkAuth()
}
// Redirect to login if not authenticated
if (!authStore.isLoggedIn) {
return navigateTo('/login')
}
}
})