Files
harheimertc/middleware/auth.global.js
2025-12-20 15:05:49 +01:00

23 lines
528 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')
}
}
})