import type { ReactNode } from "react"; import { useT } from "../i18n/I18nProvider"; import { LoginPage } from "./LoginPage"; import { useAuth } from "./AuthProvider"; import type { Role } from "./AuthProvider"; /* Route guard, now wired into router.tsx. * * This is the client-side half only. It decides what to *render*; it is not * access control. The server authorises every request independently, and the * image bytes go through the auth-checked handoff (SPEC 3.3), because an * unguessable URL is not access control either. Deleting this file would * expose no data -- it would only make the app render pages whose requests * then 401. */ export function RequireAuth({ children, roles, }: { children: ReactNode; /** When given, the signed-in user must hold one of these roles. */ roles?: Role[]; }) { const t = useT(); const { state, can } = useAuth(); if (state.status === "loading") { return

{t("common.loading")}

; } if (state.status === "anonymous") { return ; } // Now that /auth/* exists this should not happen. When it does, it means a // newer UI is being served by an older server, so showing the login form // would be offering a form that 404s. Say what is actually wrong instead. if (state.status === "unavailable") { return (

{t("auth.unavailable")}

); } if (roles !== undefined && !can(...roles)) { return (

{t("auth.forbidden")}

); } return <>{children}; }