import { oauthLoginUrl, oauthHandleRedirectIfPresent } from "@huggingface/hub"; interface OAuthResult { accessToken: string; userInfo?: { name?: string; email?: string; [key: string]: any; }; } declare global { interface Window { huggingface: { variables: { OAUTH_SCOPES: string; // Changed from string[] to string }; }; } } async function initializeAuth(): Promise { try { console.log("huggingface env", window.huggingface); let oauthResult: OAuthResult | null = null; const storedAuth = localStorage.getItem("oauth"); if (storedAuth) { try { oauthResult = JSON.parse(storedAuth); } catch (error) { console.error("Failed to parse stored OAuth data:", error); localStorage.removeItem("oauth"); } } const redirectResult = await oauthHandleRedirectIfPresent(); if (redirectResult) { oauthResult = redirectResult as OAuthResult; } if (oauthResult) { const preElement = document.querySelector("pre"); if (preElement) { preElement.textContent = JSON.stringify(oauthResult, null, 2); } localStorage.setItem("oauth", JSON.stringify(oauthResult)); const signoutButton = document.getElementById("signout"); if (signoutButton) { signoutButton.style.removeProperty("display"); signoutButton.onclick = async () => { localStorage.removeItem("oauth"); window.location.href = window.location.href.replace(/\?.*$/, ""); window.location.reload(); }; } } else { const signinButton = document.getElementById("signin"); if (signinButton) { signinButton.style.removeProperty("display"); signinButton.onclick = async () => { try { const loginUrl = await oauthLoginUrl({ scopes: window.huggingface.variables.OAUTH_SCOPES, }); window.location.href = `${loginUrl}&prompt=consent`; } catch (error) { console.error("Failed to generate login URL:", error); } }; } } } catch (error) { console.error("Authentication initialization failed:", error); } } // Initialize the authentication flow initializeAuth().catch((error) => { console.error("Fatal error during authentication initialization:", error); });