From 5c8bf15956689488c05ac89f24f818e8f32c361c Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 7 Aug 2025 14:42:05 +0200 Subject: [PATCH] fix: streamline authentication check on dashboard mount and improve routing logic --- pages/dashboard/index.vue | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/pages/dashboard/index.vue b/pages/dashboard/index.vue index 5d0bd49..575839b 100644 --- a/pages/dashboard/index.vue +++ b/pages/dashboard/index.vue @@ -21,26 +21,35 @@ definePageMeta({ middleware: 'auth' }); -const { user, userTier, isAuthenticated } = useAuth(); +const { user, userTier, isAuthenticated, checkAuth } = useAuth(); const loading = ref(true); +const authChecked = ref(false); -// Route to tier-specific dashboard -watchEffect(async () => { +// Check authentication on mount +onMounted(async () => { + console.log('🔄 Dashboard mounted, checking authentication...'); + + // Ensure auth is checked before routing + await checkAuth(); + authChecked.value = true; + + console.log('✅ Auth check complete:', { + isAuthenticated: isAuthenticated.value, + user: user.value?.email, + tier: userTier.value + }); + + // Now route based on auth status if (isAuthenticated.value && user.value) { const tierRoute = `/dashboard/${userTier.value}`; console.log('🔄 Routing to tier-specific dashboard:', tierRoute); await navigateTo(tierRoute, { replace: true }); - } else if (!isAuthenticated.value) { + } else { console.log('🔄 User not authenticated, redirecting to login'); await navigateTo('/login'); } -}); - -onMounted(() => { - // Small delay to ensure auth state is loaded - setTimeout(() => { - loading.value = false; - }, 500); + + loading.value = false; });