Files
harheimertc/middleware/auth.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

49 lines
1.7 KiB
JavaScript
Executable File

export default defineNuxtRouteMiddleware(async (to, _from) => {
// Check if route requires auth
const protectedRoutes = ['/mitgliederbereich', '/cms']
const requiresAuth = protectedRoutes.some(route => to.path.startsWith(route))
if (!requiresAuth) {
return
}
// Check auth status
try {
const { data: auth } = await useFetch('/api/auth/status')
if (!auth.value || !auth.value.isLoggedIn) {
return navigateTo('/login?redirect=' + to.path)
}
// Check role for CMS
if (to.path.startsWith('/cms')) {
const roles = auth.value.roles || (auth.value.role ? [auth.value.role] : [])
const hasAccess = roles.includes('admin') || roles.includes('vorstand') || roles.includes('newsletter')
const canAccessContactRequests = roles.includes('admin') || roles.includes('vorstand') || roles.includes('trainer')
// Newsletter-Seite nur für Newsletter-Rolle, Admin oder Vorstand
if (to.path.startsWith('/cms/newsletter')) {
if (!hasAccess) {
return navigateTo('/mitgliederbereich')
}
} else if (to.path.startsWith('/cms/passwort-reset-diagnose')) {
if (!roles.includes('admin')) {
return navigateTo('/cms')
}
} else if (to.path.startsWith('/cms/kontaktanfragen')) {
if (!canAccessContactRequests) {
return navigateTo('/mitgliederbereich')
}
} else {
// Andere CMS-Seiten nur für Admin oder Vorstand
const isAdmin = roles.includes('admin') || roles.includes('vorstand')
if (!isAdmin) {
return navigateTo('/mitgliederbereich')
}
}
}
} catch (_error) {
return navigateTo('/login?redirect=' + to.path)
}
})