feat(webui): EULA/PRIVACY consent gate — frontend
ConsentPage shown after login, before the forced password change: tabbed EULA/PRIVACY rendered by an in-house, dependency-free markdown renderer (React elements only, XSS-safe; single-pass bounded-class inline parser to avoid ReDoS on large docs). App gate order is login -> consent -> password -> app; api client gains agreements.get / recordConsent with a 409 stale-version re-prompt that refetches the new text.
This commit is contained in:
5 files changed
+583
-31
No files matched your search
+117
-31
@@ -5,7 +5,9 @@ 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 type { AgreementsPayload } from '@/lib/api/types';
|
||||
import { appRouter } from '@/router';
|
||||
|
||||
export default function App() {
|
||||
@@ -21,6 +23,10 @@ function AuthBoundary() {
|
||||
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<AgreementsPayload | null>(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
|
||||
@@ -38,6 +44,18 @@ function AuthBoundary() {
|
||||
[],
|
||||
);
|
||||
|
||||
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 () => {
|
||||
const ok = await client.status();
|
||||
@@ -45,54 +63,88 @@ function AuthBoundary() {
|
||||
setAuthed(true);
|
||||
setStatus('已连接');
|
||||
setMustChange(await client.mustChangePassword());
|
||||
await refreshAgreements();
|
||||
}
|
||||
setAuthChecked(true);
|
||||
})();
|
||||
}, [client]);
|
||||
}, [client, refreshAgreements]);
|
||||
|
||||
const handleLogoutComplete = useCallback(() => {
|
||||
const handleLoggedOut = useCallback(() => {
|
||||
// Reset the URL so the next login lands on the overview page, matching
|
||||
// the pre-router behaviour.
|
||||
// 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 = <Splash>初始化中…</Splash>;
|
||||
} else if (!authed) {
|
||||
view = (
|
||||
<LoginGate
|
||||
onAuthed={(needsChange, password) => {
|
||||
setAuthed(true);
|
||||
setStatus('已连接');
|
||||
setMustChange(needsChange);
|
||||
setLoginPassword(password);
|
||||
void refreshAgreements();
|
||||
}}
|
||||
/>
|
||||
);
|
||||
} else if (agreements === null) {
|
||||
view = <Splash>加载中…</Splash>;
|
||||
} else if (needsConsent) {
|
||||
view = (
|
||||
<ConsentGate
|
||||
payload={agreements}
|
||||
onAccepted={() => setNeedsConsent(false)}
|
||||
onStale={refreshAgreements}
|
||||
onDecline={handleDecline}
|
||||
/>
|
||||
);
|
||||
} else if (mustChange) {
|
||||
view = (
|
||||
<ForcedChangePasswordGate
|
||||
knownOldPassword={loginPassword}
|
||||
onSuccess={() => {
|
||||
setMustChange(false);
|
||||
setLoginPassword(undefined);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
} else {
|
||||
view = (
|
||||
<SessionProvider value={{ status, onLogoutComplete: handleLoggedOut }}>
|
||||
<RouterProvider router={appRouter} />
|
||||
</SessionProvider>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<ApiProvider client={client}>
|
||||
<TooltipProvider delayDuration={150}>
|
||||
{!authChecked ? (
|
||||
<div className="flex min-h-screen items-center justify-center bg-background text-sm text-muted-foreground">
|
||||
初始化中…
|
||||
</div>
|
||||
) : !authed ? (
|
||||
<LoginGate
|
||||
onAuthed={(needsChange, password) => {
|
||||
setAuthed(true);
|
||||
setStatus('已连接');
|
||||
setMustChange(needsChange);
|
||||
setLoginPassword(password);
|
||||
}}
|
||||
/>
|
||||
) : mustChange ? (
|
||||
<ForcedChangePasswordGate
|
||||
knownOldPassword={loginPassword}
|
||||
onSuccess={() => {
|
||||
setMustChange(false);
|
||||
setLoginPassword(undefined);
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<SessionProvider value={{ status, onLogoutComplete: handleLogoutComplete }}>
|
||||
<RouterProvider router={appRouter} />
|
||||
</SessionProvider>
|
||||
)}
|
||||
</TooltipProvider>
|
||||
<TooltipProvider delayDuration={150}>{view}</TooltipProvider>
|
||||
</ApiProvider>
|
||||
);
|
||||
}
|
||||
|
||||
function Splash({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center bg-background text-sm text-muted-foreground">
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function LoginGate({ onAuthed }: { onAuthed: (mustChange: boolean, password: string) => void }) {
|
||||
const api = useApi();
|
||||
const handleLogin = useCallback(
|
||||
@@ -107,6 +159,40 @@ function LoginGate({ onAuthed }: { onAuthed: (mustChange: boolean, password: str
|
||||
return <LoginPage onLogin={handleLogin} />;
|
||||
}
|
||||
|
||||
function ConsentGate({
|
||||
payload,
|
||||
onAccepted,
|
||||
onStale,
|
||||
onDecline,
|
||||
}: {
|
||||
payload: AgreementsPayload;
|
||||
onAccepted: () => void;
|
||||
onStale: () => void;
|
||||
onDecline: () => void;
|
||||
}) {
|
||||
const api = useApi();
|
||||
return (
|
||||
<ConsentPage
|
||||
documents={payload.documents}
|
||||
version={payload.version}
|
||||
onDecline={onDecline}
|
||||
onAccept={async () => {
|
||||
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,
|
||||
|
||||
Reference in new issue
Block a user