fix: auth race condition on refresh, section edit dialog, time slot duplicate, autocomplete disable
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,19 +1,48 @@
|
||||
import type { Router } from 'vue-router'
|
||||
import { useAuthStore } from '@/stores/useAuthStore'
|
||||
import { useOrganisationStore } from '@/stores/useOrganisationStore'
|
||||
|
||||
export function setupGuards(router: Router) {
|
||||
router.beforeEach((to) => {
|
||||
router.beforeEach(async (to) => {
|
||||
const authStore = useAuthStore()
|
||||
const isPublic = to.meta.public === true
|
||||
|
||||
// Guest-only pages (login): redirect to home if already authenticated
|
||||
if (isPublic && authStore.isAuthenticated) {
|
||||
return { name: 'dashboard' }
|
||||
// Wait for initialization to complete (only blocks on first navigation)
|
||||
if (!authStore.isInitialized) {
|
||||
await authStore.initialize()
|
||||
}
|
||||
|
||||
// Protected pages: redirect to login if not authenticated
|
||||
if (!isPublic && !authStore.isAuthenticated && to.meta.requiresAuth !== false) {
|
||||
const isPublic = to.meta.public === true
|
||||
|
||||
// Allow public routes (login, 404) — but redirect authenticated users away from login
|
||||
if (isPublic) {
|
||||
if (authStore.isAuthenticated && to.path === '/login') {
|
||||
return { name: 'dashboard' }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Routes that opt out of auth (e.g. invitations)
|
||||
if (to.meta.requiresAuth === false) {
|
||||
return
|
||||
}
|
||||
|
||||
// Not authenticated → redirect to login with return URL
|
||||
if (!authStore.isAuthenticated) {
|
||||
return { path: '/login', query: { to: to.fullPath } }
|
||||
}
|
||||
|
||||
// Authenticated — check organisation selection for routes that need it
|
||||
const orgStore = useOrganisationStore()
|
||||
const isSelectOrgPage = to.path === '/select-organisation'
|
||||
|
||||
if (isSelectOrgPage) {
|
||||
// Already on the org selection page — allow
|
||||
return
|
||||
}
|
||||
|
||||
// If user has organisations but none selected → redirect to selection
|
||||
if (authStore.organisations.length > 0 && !orgStore.hasOrganisation) {
|
||||
return { path: '/select-organisation' }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user