diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index eb71082..b72eb23 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -6,6 +6,7 @@ import React, { useState, useEffect, useTransition, useCallback, lazy } from 're import { useQueryClient } from '@tanstack/react-query'; import { Routes, Route, Navigate } from 'react-router-dom'; import { useAuth0 } from '@auth0/auth0-react'; +import { useIsAuthInitialized } from './core/auth/auth-gate'; import { motion, AnimatePresence } from 'framer-motion'; import { ThemeProvider } from '@mui/material/styles'; import CssBaseline from '@mui/material/CssBaseline'; @@ -237,7 +238,9 @@ const AddVehicleScreen: React.FC = ({ onBack, onAdded }) function App() { const { isLoading, isAuthenticated, user } = useAuth0(); + const isAuthGateReady = useIsAuthInitialized(); const [_isPending, startTransition] = useTransition(); + console.log('[DEBUG App] Render check - isLoading:', isLoading, 'isAuthenticated:', isAuthenticated, 'isAuthGateReady:', isAuthGateReady); // Initialize data synchronization const { prefetchForNavigation } = useDataSync(); @@ -380,6 +383,32 @@ function App() { ); } + // Wait for auth gate to be ready before rendering protected routes + // This prevents a race condition where the page renders before the auth token is ready + if (!isAuthGateReady) { + console.log('[DEBUG App] Auth gate not ready yet, showing loading state'); + if (mobileMode) { + return ( + + + +
+
Initializing session...
+
+
+
+ ); + } + return ( + + +
+
Initializing session...
+
+
+ ); + } + // Mobile app rendering if (mobileMode) { return (