Add backend health check, fetch timeouts, stale token cleanup, and error screen

Frontend now checks /health before starting auth flow. Shows a clear
"Cannot Connect to Server" screen with retry button when backend is
unreachable. Stale non-JWT tokens in localStorage are detected and
cleared automatically. All API calls have a 10s timeout via AbortController.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-27 19:55:50 -04:00
parent 066c3310ff
commit 2ab097ec8a
4 changed files with 117 additions and 43 deletions
+34 -3
View File
@@ -7,7 +7,7 @@ import { InviteGateScreen } from '@/components/game/InviteGateScreen';
import { useGameLoop } from '@/hooks/useGameLoop';
import { useAuthGate } from '@/hooks/useAuthGate';
import { TICK_INTERVAL_MS } from '@ai-tycoon/shared';
import { Sparkles } from 'lucide-react';
import { Sparkles, RefreshCw, WifiOff } from 'lucide-react';
function LoadingScreen() {
return (
@@ -25,8 +25,35 @@ function LoadingScreen() {
);
}
function BackendErrorScreen({ error, onRetry }: { error: string; onRetry: () => void }) {
return (
<div className="min-h-screen flex items-center justify-center bg-gradient-to-b from-surface-950 to-surface-900">
<div className="max-w-md w-full mx-4 text-center">
<div className="inline-flex items-center gap-2 mb-6">
<Sparkles className="text-accent-light" size={32} />
<h1 className="text-4xl font-bold bg-gradient-to-r from-accent-light to-accent bg-clip-text text-transparent">
AI Tycoon
</h1>
</div>
<div className="bg-surface-900 border border-surface-700 rounded-xl p-6 space-y-4">
<WifiOff className="mx-auto text-danger" size={40} />
<h2 className="text-lg font-semibold text-surface-100">Cannot Connect to Server</h2>
<p className="text-sm text-surface-400">{error}</p>
<button
onClick={onRetry}
className="inline-flex items-center gap-2 px-5 py-2.5 rounded-lg bg-accent hover:bg-accent-dark text-white font-medium text-sm transition-colors"
>
<RefreshCw size={16} /> Retry
</button>
</div>
</div>
</div>
);
}
export function App() {
const { loading: authLoading, needsInvite, needsPasswordReset, setRegistered, setNeedsPasswordReset } = useAuthGate();
const { loading: authLoading, backendError, needsInvite, needsPasswordReset, setRegistered, setNeedsPasswordReset, retry } = useAuthGate();
const companyName = useGameStore((s) => s.meta.companyName);
const lastTickTimestamp = useGameStore((s) => s.meta.lastTickTimestamp);
const [catchUpTicks, setCatchUpTicks] = useState<number | null>(null);
@@ -43,12 +70,16 @@ export function App() {
}
}, [companyName, lastTickTimestamp, catchUpDone]);
useGameLoop(!catchUpDone || authLoading || needsInvite || needsPasswordReset);
useGameLoop(!catchUpDone || authLoading || !!backendError || needsInvite || needsPasswordReset);
if (authLoading) {
return <LoadingScreen />;
}
if (backendError) {
return <BackendErrorScreen error={backendError} onRetry={retry} />;
}
if (needsInvite || needsPasswordReset) {
return (
<InviteGateScreen