Spaces:
Running
Running
Download src/auth.ts from dsh-marl/trajectory-explorer: direct link, hf CLI and curl.
- Browser
- Download file 1.62 kB
-
https://huggingface.co/spaces/dsh-marl/trajectory-explorer/resolve/main/src/auth.ts
- Command line
-
hf download hf://spaces/dsh-marl/trajectory-explorer/src/auth.ts
-
curl -L -o auth.ts https://huggingface.co/spaces/dsh-marl/trajectory-explorer/resolve/main/src/auth.ts
1.62 kB
| // "Sign in with Hugging Face" for the static Space. The Space README sets hf_oauth with the read-repos scope and | |
| // restricts sign-in to members of dsh-marl; the access token then reads the private dataset from the browser. | |
| import { oauthHandleRedirectIfPresent, oauthLoginUrl } from "@huggingface/hub"; | |
| export interface Session { token: string; expires: number; user: string; avatar: string } | |
| const KEY = "dsh-te:session"; | |
| const save = (s: Session | null) => { try { s ? localStorage.setItem(KEY, JSON.stringify(s)) : localStorage.removeItem(KEY); } catch { /* storage blocked */ } }; | |
| export async function restoreSession(): Promise<Session | null> { | |
| const r = await oauthHandleRedirectIfPresent(); | |
| if (r) { | |
| const s: Session = { token: r.accessToken, expires: r.accessTokenExpiresAt.getTime(), user: r.userInfo.preferred_username || r.userInfo.name, avatar: r.userInfo.picture }; | |
| save(s); | |
| history.replaceState(null, "", location.pathname + (r.state && r.state.startsWith("#") ? r.state : "")); // drop ?code=..., go back to the page we left | |
| return s; | |
| } | |
| try { | |
| const s = JSON.parse(localStorage.getItem(KEY) || "null") as Session | null; | |
| if (s && s.expires > Date.now() + 60_000) return s; | |
| } catch { /* storage blocked */ } | |
| save(null); | |
| return null; | |
| } | |
| export const inIframe = () => { try { return window.self !== window.top; } catch { return true; } }; | |
| export async function signIn() { | |
| location.href = await oauthLoginUrl({ redirectUrl: location.origin + location.pathname, state: location.hash || "#/" }); | |
| } | |
| export function signOut() { save(null); location.reload(); } | |