41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
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')
|
|
|
|
// Newsletter-Seite nur für Newsletter-Rolle, Admin oder Vorstand
|
|
if (to.path.startsWith('/cms/newsletter')) {
|
|
if (!hasAccess) {
|
|
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)
|
|
}
|
|
})
|
|
|