Download webui/src/features/auth/RequireAuth.tsx from xjackkx/rg: direct link, hf CLI and curl.
- Browser
- Download file 1.6 kB
-
https://huggingface.co/spaces/xjackkx/rg/resolve/main/webui/src/features/auth/RequireAuth.tsx
- Command line
-
hf download hf://spaces/xjackkx/rg/webui/src/features/auth/RequireAuth.tsx
-
curl -L -o RequireAuth.tsx https://huggingface.co/spaces/xjackkx/rg/resolve/main/webui/src/features/auth/RequireAuth.tsx
1.6 kB
| import { useEffect, useState, type ReactElement } from "react"; | |
| import { Navigate, useLocation } from "react-router-dom"; | |
| import { useAuthStore } from "./auth-store"; | |
| type RequireAuthProps = { | |
| children: ReactElement; | |
| }; | |
| export function RequireAuth({ children }: RequireAuthProps) { | |
| const token = useAuthStore((state) => state.token); | |
| const location = useLocation(); | |
| const [checked, setChecked] = useState(Boolean(token)); | |
| const [anonymousAllowed, setAnonymousAllowed] = useState(false); | |
| useEffect(() => { | |
| if (token) { | |
| setAnonymousAllowed(false); | |
| setChecked(true); | |
| return; | |
| } | |
| let active = true; | |
| const controller = new AbortController(); | |
| const checkAuthMode = async () => { | |
| try { | |
| const response = await fetch("/api/v1/system/info", { | |
| method: "GET", | |
| signal: controller.signal, | |
| }); | |
| if (!active) { | |
| return; | |
| } | |
| // /api/v1/system/info returns 200 only when admin auth is disabled. | |
| setAnonymousAllowed(response.ok); | |
| } catch { | |
| if (!active) { | |
| return; | |
| } | |
| setAnonymousAllowed(false); | |
| } finally { | |
| if (active) { | |
| setChecked(true); | |
| } | |
| } | |
| }; | |
| void checkAuthMode(); | |
| return () => { | |
| active = false; | |
| controller.abort(); | |
| }; | |
| }, [token]); | |
| if (token || anonymousAllowed) { | |
| return children; | |
| } | |
| if (!checked) { | |
| return null; | |
| } | |
| const next = `${location.pathname}${location.search}`; | |
| return <Navigate to={`/login?next=${encodeURIComponent(next)}`} replace />; | |
| } | |