xxxxxxk's picture
Trajectory explorer: overview, training, comparison, episode view, HF sign-in
028a5cd verified
Raw History Blame Contribute Delete
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(); }