Files
harheimertc/middleware/auth.global.js
Torsten Schulz (local) 3586704cce
Some checks failed
Code Analysis and Production Deploy / analyze (push) Failing after 10m29s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Has been skipped
Update file permissions for multiple server and test files to executable mode
2026-07-15 08:45:12 +02:00

22 lines
774 B
JavaScript
Executable File

export default defineNuxtRouteMiddleware(async (to, _from) => {
// Check if route requires authentication
const mw = to.meta.middleware
const requiresAuth =
mw === 'auth' || (Array.isArray(mw) && mw.includes('auth'))
if (requiresAuth) {
// Nicht auf Pinia angewiesen sein (sonst "no active Pinia" in manchen Nuxt-Lifecycle-Phasen)
try {
const { data: auth } = await useFetch('/api/auth/status')
if (!auth.value || !auth.value.isLoggedIn) {
const redirect = encodeURIComponent(to.fullPath || to.path || '/')
return navigateTo(`/login?redirect=${redirect}`)
}
} catch (_e) {
const redirect = encodeURIComponent(to.fullPath || to.path || '/')
return navigateTo(`/login?redirect=${redirect}`)
}
}
})