Auth Race Condition Fix
Problem
Data fetching hooks (useRestaurants, useRestaurant, useMenuItems) fired SWR requests immediately on mount, before the Supabase auth session was ready. This caused:
- “Failed to load restaurants” on first page load
- Menu images failing to load
- Manager/admin views showing empty content
- All fixed by a page refresh (session established by then)
Solution
Gate SWR keys behind useAuth().loading — when loading is true, SWR key is null (no request fires).
const { loading: authLoading } = useAuth();
const ready = enabled && !authLoading;
const swr = useSWR<Restaurant[]>(ready ? ["restaurants"] : null, () => listActiveRestaurants());
Files Modified
src/hooks/useRestaurants.ts—useRestaurantsanduseRestauranthookssrc/hooks/useMenuItems.ts—useMenuItemshook