export default defineNuxtRouteMiddleware(async (to, from) => { const auth = useAuthStore() const token = useCookie("token", { path: "/" }).value // 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 (!auth.user && token && process.client) { 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") { if (auth.user.must_change_password) { return navigateTo("/password-change") } return navigateTo(isPortalUser ? "/customer-portal" : "/") } 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") } })