import { useCallback, useEffect, useMemo, useState } from 'react'; import { RouterProvider } from '@tanstack/react-router'; import { TooltipProvider } from '@/components/ui/tooltip'; import { ThemeProvider } from '@/contexts/ThemeContext'; import { SessionProvider } from '@/contexts/SessionContext'; import { LoginPage } from '@/components/pages/login-page'; import { ChangePasswordPage } from '@/components/pages/change-password-page'; import { ConsentPage } from '@/components/pages/consent-page'; import { ApiProvider, createApiClient, useApi, type ApiClient } from '@/lib/api'; import { DebugTaskProvider } from '@/contexts/DebugTaskContext'; import { TaskBadge } from '@/components/debug/task-badge'; import type { AgreementsPayload } from '@/lib/api/types'; import { appRouter } from '@/router'; export default function App() { return ( ); } // #194: read a one-shot `?token=` login param and immediately strip // it from the URL (replaceState) so the credential doesn't linger in the // address bar / browser history / Referer header. Named `token` to match the // requested query key; the value is the WebUI login password. function consumeUrlToken(): string | null { if (typeof window === 'undefined') return null; try { const url = new URL(window.location.href); const value = url.searchParams.get('token'); if (!value) return null; url.searchParams.delete('token'); window.history.replaceState({}, '', `${url.pathname}${url.search}${url.hash}`); return value; } catch { return null; } } function AuthBoundary() { const [authChecked, setAuthChecked] = useState(false); const [authed, setAuthed] = useState(false); const [mustChange, setMustChange] = useState(false); const [status, setStatus] = useState('未连接'); // Agreement consent gate, shown after login but BEFORE the forced password // change. `agreements === null` while the post-auth fetch is in flight. const [agreements, setAgreements] = useState(null); const [needsConsent, setNeedsConsent] = useState(false); // The password from *this* session's login, carried into the forced // change-password gate so it doesn't have to render an old-password field // (which browsers autofill, misleading users on upgrade). Stays undefined // for a returning session that's already authed but still must change. const [loginPassword, setLoginPassword] = useState(undefined); const client = useMemo( () => createApiClient({ onUnauthorized: () => { setAuthed(false); setStatus('未授权'); }, }), [], ); const refreshAgreements = useCallback(async () => { try { const payload = await client.agreements.get(); setAgreements(payload); setNeedsConsent(payload.consentRequired); } catch { // Fail open on a fetch error so a transient hiccup can't wedge the gate. setAgreements({ version: '', consentRequired: false, documents: [] }); setNeedsConsent(false); } }, [client]); useEffect(() => { (async () => { let ok = await client.status(); // #194: `?token=` in the URL logs in without typing the // password. Only attempted when no stored token already authed us; the // value is the WebUI login password (verified by /api/login server-side, // so a wrong one just falls through to the login page). The param is // consumed once and stripped from the URL first — a password in the // address bar leaks via browser history / access logs / the Referer. let urlPassword: string | undefined; if (!ok) { const pw = consumeUrlToken(); if (pw) { const result = await client.login(pw); if (result.ok) { ok = true; urlPassword = pw; } } } if (ok) { setAuthed(true); setStatus('已连接'); setMustChange(await client.mustChangePassword()); // Carry the URL password into the forced change-password gate so it // needn't re-prompt for the old password (matches the login flow). if (urlPassword) setLoginPassword(urlPassword); await refreshAgreements(); } setAuthChecked(true); })(); }, [client, refreshAgreements]); const handleLoggedOut = useCallback(() => { // Reset the URL so the next login lands on the overview page, matching // the pre-router behaviour, and clear every post-auth gate. window.history.replaceState({}, '', '/'); setAuthed(false); setStatus('未连接'); setMustChange(false); setAgreements(null); setNeedsConsent(false); setLoginPassword(undefined); }, []); const handleDecline = useCallback(async () => { await client.logout(); handleLoggedOut(); }, [client, handleLoggedOut]); let view: React.ReactNode; if (!authChecked) { view = 初始化中…; } else if (!authed) { view = ( { setAuthed(true); setStatus('已连接'); setMustChange(needsChange); setLoginPassword(password); void refreshAgreements(); }} /> ); } else if (agreements === null) { view = 加载中…; } else if (needsConsent) { view = ( setNeedsConsent(false)} onStale={refreshAgreements} onDecline={handleDecline} /> ); } else if (mustChange) { view = ( { setMustChange(false); setLoginPassword(undefined); }} /> ); } else { view = ( ); } return ( {view} ); } function Splash({ children }: { children: React.ReactNode }) { return (
{children}
); } function LoginGate({ onAuthed }: { onAuthed: (mustChange: boolean, password: string) => void }) { const api = useApi(); const handleLogin = useCallback( async (password: string) => { const result = await api.login(password); if (!result.ok) return { success: false, error: result.message }; onAuthed(result.mustChangePassword, password); return { success: true }; }, [api, onAuthed], ); return ; } function ConsentGate({ payload, onAccepted, onStale, onDecline, }: { payload: AgreementsPayload; onAccepted: () => void; onStale: () => void; onDecline: () => void; }) { const api = useApi(); return ( { const result = await api.agreements.recordConsent(payload.version); if (result.success) { onAccepted(); return { success: true }; } // 409: the agreement text changed under us — re-fetch and re-prompt. if (result.currentVersion && result.currentVersion !== payload.version) { onStale(); return { success: false, message: '协议已更新,已为你载入最新版本,请重新阅读后确认。' }; } return { success: false, message: result.message ?? '提交失败,请重试' }; }} /> ); } function ForcedChangePasswordGate({ knownOldPassword, onSuccess, }: { knownOldPassword?: string; onSuccess: () => void; }) { const api = useApi(); return ( api.checkPasswordStrength(p)} submit={(o, n) => api.changePassword(o, n)} onSuccess={onSuccess} /> ); }