Files
crewli/apps/portal/src/plugins/1.router/guards.ts
bert.hausmans 821bfc5bcf chore: standardize dev-only debug logging across all three SPAs
Router guards:
- apps/app: added DEV-gated logging matching admin pattern
  (route info, auth decisions, org selection, access granted/denied)
- apps/portal: added DEV-gated logging matching admin pattern
  (route info, auth decisions, backward-compat redirects)
- apps/admin: already had full logging (unchanged)

Ungated console statements fixed:
- admin/main.ts: error handler, plugin registration, mount errors
- admin/pages/login.vue, register.vue: catch block errors
- admin/pages/events/index.vue: fetch error logging
- admin/pages/wizard-examples: demo form submit logging
- admin/pages/faq.vue: catch block error

All console statements in Crewli-authored code are now gated behind
import.meta.env.DEV — zero console output in production builds.
Vuexy template demo files (views/demos/*) left as-is.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 16:43:31 +02:00

65 lines
2.1 KiB
TypeScript

import type { Router } from 'vue-router'
import { useAuthStore } from '@/stores/useAuthStore'
import { usePortalStore } from '@/stores/usePortalStore'
const guestOnlyPaths = ['/login', '/wachtwoord-vergeten', '/wachtwoord-resetten', '/verify-email-change']
// Old dashboard routes that need backward-compat redirects
const dashboardRedirects: Record<string, string> = {
'/dashboard': '/evenementen',
'/dashboard/my-shifts': '/evenementen',
'/dashboard/claim-shifts': '/evenementen',
}
export function setupGuards(router: Router) {
router.beforeEach(async (to, from) => {
const authStore = useAuthStore()
if (!authStore.isInitialized) {
await authStore.initialize()
}
if (import.meta.env.DEV) {
console.log('🔒 Router Guard:', {
to: to.path,
from: from.path,
isAuthenticated: authStore.isAuthenticated,
})
}
// Hydrate portal data once after auth is confirmed
if (authStore.isAuthenticated) {
const portalStore = usePortalStore()
await portalStore.hydrateIfNeeded()
}
// Backward-compat redirects for old dashboard routes
const redirect = dashboardRedirects[to.path]
if (redirect && authStore.isAuthenticated) {
if (import.meta.env.DEV) console.log('🔄 Backward-compat redirect:', to.path, '→', redirect)
return { path: redirect }
}
const requiresAuth = to.meta.requiresAuth === true
// Public routes — no auth check needed
if (!requiresAuth) {
if (authStore.isAuthenticated && guestOnlyPaths.some(p => to.path === p || to.path.startsWith(`${p}/`))) {
if (import.meta.env.DEV) console.log('🔄 Redirecting logged-in user away from login page')
return { path: '/evenementen' }
}
if (import.meta.env.DEV) console.log('✅ Public route, allowing access')
return
}
// Auth required — redirect to login if not authenticated
if (!authStore.isAuthenticated) {
if (import.meta.env.DEV) console.log('🚫 Not authenticated, redirecting to login')
return { path: '/login', query: { to: to.fullPath } }
}
if (import.meta.env.DEV) console.log('✅ Access granted')
})
}