Files
FEDEO/frontend/middleware/auth.global.ts
flfeders 5463106938
All checks were successful
Build and Release FEDEO / verify (push) Successful in 1m7s
Build and Release FEDEO / build-and-release (push) Successful in 6m24s
fix(portal): add invitation links and correct customer login routing
2026-10-13 18:13:41 +02:00

59 lines
1.6 KiB
TypeScript

export default defineNuxtRouteMiddleware(async (to, from) => {
// Guest sessions are handled only by the dedicated collaboration guest API.
if (to.path === '/guest') return
const auth = useAuthStore()
// DEBUG: Was sieht die Middleware wirklich?
console.log("🔒 Middleware Check auf:", to.path)
console.log("👤 User Status:", auth.user ? "Eingeloggt" : "Gast")
// 1. Ausnahme für Workflows
// WICHTIG: Prüfen ob to.path wirklich mit /workflows beginnt
if (to.path.startsWith('/workflows')) {
console.log("✅ Zugriff erlaubt (Public Route)")
return
}
if (process.server) {
return
}
const token = auth.getStoredToken()
if (!auth.user && token) {
console.log("🔄 Auth-Bootstrap aus Cookie")
await auth.initStore()
}
const isPortalUser = Boolean(auth.profile?.customer_for_portal)
if (auth.loading) {
console.log("⏳ Auth lädt noch...")
return
}
// 2. Wenn nicht eingeloggt
if (!auth.user) {
// Erlaube Zugriff auf Login/Reset Seiten
if (["/login", "/password-reset"].includes(to.path)) {
return
}
console.log("⛔ Blocked: Not logged in - Redirect to /login")
return navigateTo("/login")
}
// 3. Wenn eingeloggt
if (to.path === "/login") {
return navigateTo(auth.getLandingPath())
}
if (auth.user.must_change_password && to.path !== "/password-change") {
return navigateTo("/password-change")
}
if (isPortalUser && !["/customer-portal", "/password-change"].includes(to.path)) {
return navigateTo("/customer-portal")
}
})