Spaces:
Sleeping
Sleeping
File size: 11,255 Bytes
8893529 363bda3 8893529 363bda3 8893529 363bda3 8893529 363bda3 8893529 363bda3 8893529 363bda3 8893529 363bda3 8893529 363bda3 8893529 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | /**
* Auth: Hugging Face (when on HF Spaces) or app account (create account / sign in with email).
* Commenting requires being signed in one way or the other.
*/
(function () {
const HF_STORAGE_KEY = "yantrabodha_hf_oauth";
var supabase = null;
window.getHfUser = function () {
return window.__HF_USER__ || null;
};
window.getAppUser = function () {
return window.__APP_USER__ || null;
};
function setAppUserFromSession(session) {
if (!session || !session.user) { window.__APP_USER__ = null; return; }
var user = session.user;
var name = (user.user_metadata && (user.user_metadata.name || user.user_metadata.full_name)) || user.email;
window.__APP_USER__ = { name: name || user.email, email: user.email, token: session.access_token };
}
window.getCurrentUser = function () {
return window.getHfUser() || window.getAppUser();
};
window.signOutHf = function () {
try { sessionStorage.removeItem(HF_STORAGE_KEY); } catch (_) {}
window.__HF_USER__ = null;
renderAuthUI();
if (typeof window.__onHfAuthChange === "function") window.__onHfAuthChange();
};
window.signOutApp = function () {
if (window.__SUPABASE_CLIENT__) {
window.__SUPABASE_CLIENT__.auth.signOut().then(function () {
renderAuthUI();
if (typeof window.__onHfAuthChange === "function") window.__onHfAuthChange();
});
}
};
function renderAuthUI() {
var user = window.getCurrentUser();
var signin = document.getElementById("auth-signin");
var loggedin = document.getElementById("auth-loggedin");
var loggedinName = document.getElementById("auth-loggedin-name");
var loggedinImg = document.getElementById("auth-loggedin-img");
var sidebarAuthMobile = document.getElementById("sidebar-auth-mobile");
var hfBtn = document.getElementById("auth-signin-btn");
if (hfBtn) hfBtn.style.display = (window.huggingface && window.huggingface.variables) ? "" : "none";
var sep = document.querySelector(".auth-sep");
if (sep) sep.style.display = (window.huggingface && window.huggingface.variables) ? "" : "none";
if (!signin && !loggedin) return;
if (user) {
if (signin) signin.style.display = "none";
if (typeof window.__updateTabsForAuth === "function") window.__updateTabsForAuth();
if (loggedin) {
loggedin.style.display = "flex";
if (loggedinName) loggedinName.textContent = user.name || user.email || "User";
if (loggedinImg) {
if (user.picture) {
loggedinImg.src = user.picture;
loggedinImg.alt = "";
loggedinImg.style.display = "";
} else {
loggedinImg.style.display = "none";
}
}
}
if (sidebarAuthMobile) {
sidebarAuthMobile.classList.add("is-logged-in");
sidebarAuthMobile.setAttribute("aria-hidden", "false");
}
} else {
if (signin) signin.style.display = "flex";
if (loggedin) loggedin.style.display = "none";
if (typeof window.__updateTabsForAuth === "function") window.__updateTabsForAuth();
if (sidebarAuthMobile) {
sidebarAuthMobile.classList.remove("is-logged-in");
sidebarAuthMobile.setAttribute("aria-hidden", "true");
}
}
}
function persistAndRender(result) {
var user = result && result.userInfo ? {
sub: result.userInfo.sub,
name: result.userInfo.name,
preferred_username: result.userInfo.preferred_username,
picture: result.userInfo.picture,
} : null;
try { if (user) sessionStorage.setItem(HF_STORAGE_KEY, JSON.stringify(user)); } catch (_) {}
window.__HF_USER__ = user;
renderAuthUI();
if (typeof window.__onHfAuthChange === "function") window.__onHfAuthChange();
}
async function doHfLogin() {
try {
var hub = await import("https://esm.sh/@huggingface/hub@0.21.0");
var scopes = window.huggingface && window.huggingface.variables && window.huggingface.variables.OAUTH_SCOPES;
var url = await hub.oauthLoginUrl(scopes ? { scopes: scopes } : {});
window.location.href = url + (url.indexOf("?") >= 0 ? "&" : "?") + "prompt=consent";
} catch (err) { console.warn("HF OAuth login failed:", err); }
}
function openModal(panel) {
var modal = document.getElementById("auth-modal");
if (!modal) return;
modal.hidden = false;
modal.setAttribute("aria-hidden", "false");
document.body.classList.add("auth-modal-open");
document.getElementById("auth-modal-signin").style.display = panel === "signin" ? "block" : "none";
document.getElementById("auth-modal-register").style.display = panel === "register" ? "block" : "none";
document.getElementById("auth-modal-error").style.display = "none";
document.addEventListener("keydown", onModalEscape);
}
function closeModal() {
var modal = document.getElementById("auth-modal");
if (modal) {
modal.hidden = true;
modal.setAttribute("aria-hidden", "true");
}
document.body.classList.remove("auth-modal-open");
document.removeEventListener("keydown", onModalEscape);
}
function onModalEscape(e) {
if (e.key === "Escape") closeModal();
}
function showAuthError(msg) {
var el = document.getElementById("auth-modal-error");
if (el) { el.textContent = msg || ""; el.style.display = msg ? "block" : "none"; }
}
async function initSupabase() {
try {
var r = await fetch("/api/config");
var config = await r.json();
if (!config.supabaseUrl || !config.supabaseAnonKey) return;
var mod = await import("https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2/+esm");
var createClient = mod.createClient || (mod.default && mod.default.createClient);
window.__SUPABASE_CLIENT__ = createClient(config.supabaseUrl, config.supabaseAnonKey);
supabase = window.__SUPABASE_CLIENT__;
supabase.auth.onAuthStateChange(function (event, session) {
setAppUserFromSession(session);
renderAuthUI();
if (typeof window.__onHfAuthChange === "function") window.__onHfAuthChange();
});
var session = (await supabase.auth.getSession()).data.session;
setAppUserFromSession(session);
if (session) { renderAuthUI(); if (typeof window.__onHfAuthChange === "function") window.__onHfAuthChange(); }
} catch (_) {}
}
async function init() {
var stored = null;
try { var raw = sessionStorage.getItem(HF_STORAGE_KEY); if (raw) stored = JSON.parse(raw); } catch (_) {}
if (stored) { window.__HF_USER__ = stored; renderAuthUI(); }
await initSupabase();
if (window.huggingface && window.huggingface.variables) {
try {
var hub = await import("https://esm.sh/@huggingface/hub@0.21.0");
var oauthResult = await hub.oauthHandleRedirectIfPresent();
if (oauthResult) { persistAndRender(oauthResult); bindButtons(); return; }
} catch (e) { console.warn("HF OAuth redirect failed:", e); }
}
renderAuthUI();
bindButtons();
if (typeof window.__updateTabsForAuth === "function") window.__updateTabsForAuth();
}
function bindButtons() {
var signinBtn = document.getElementById("auth-signin-btn");
if (signinBtn) signinBtn.onclick = doHfLogin;
document.body.addEventListener("click", function (e) {
if (e.target.closest(".hf-signin-btn")) { e.preventDefault(); doHfLogin(); }
});
function doSignOut() {
if (window.getHfUser()) window.signOutHf();
else window.signOutApp();
var leftSidebar = document.getElementById("left-sidebar");
if (leftSidebar) leftSidebar.classList.remove("is-open");
}
var signoutBtn = document.getElementById("auth-signout-btn");
if (signoutBtn) signoutBtn.onclick = doSignOut;
var signoutSidebarBtn = document.getElementById("auth-signout-sidebar-btn");
if (signoutSidebarBtn) signoutSidebarBtn.onclick = doSignOut;
var appSigninBtn = document.getElementById("auth-app-signin-btn");
if (appSigninBtn) appSigninBtn.onclick = function () { openModal("signin"); };
var appRegisterBtn = document.getElementById("auth-app-register-btn");
if (appRegisterBtn) appRegisterBtn.onclick = function () { openModal("register"); };
document.getElementById("auth-show-register") && document.getElementById("auth-show-register").addEventListener("click", function () { openModal("register"); });
document.getElementById("auth-show-signin") && document.getElementById("auth-show-signin").addEventListener("click", function () { openModal("signin"); });
var closeBtn = document.querySelector(".auth-modal-close");
if (closeBtn) closeBtn.addEventListener("click", closeModal);
var backdrop = document.querySelector(".auth-modal-backdrop");
if (backdrop) backdrop.addEventListener("click", closeModal);
var formSignin = document.getElementById("auth-form-signin");
if (formSignin) {
formSignin.onsubmit = async function (e) {
e.preventDefault();
showAuthError("");
if (!window.__SUPABASE_CLIENT__) { showAuthError("App sign-in not configured."); return; }
var email = document.getElementById("auth-email-signin").value.trim();
var password = document.getElementById("auth-password-signin").value;
try {
var res = await window.__SUPABASE_CLIENT__.auth.signInWithPassword({ email: email, password: password });
if (res.error) throw new Error(res.error.message);
closeModal();
setAppUserFromSession(res.data.session);
renderAuthUI();
if (typeof window.__onHfAuthChange === "function") window.__onHfAuthChange();
} catch (err) {
var msg = err.message || "Sign in failed.";
if (msg.toLowerCase().indexOf("email not confirmed") >= 0) {
msg = "Please confirm your email first. Check your inbox for the confirmation link, then try again.";
}
showAuthError(msg);
}
};
}
var formRegister = document.getElementById("auth-form-register");
if (formRegister) {
formRegister.onsubmit = async function (e) {
e.preventDefault();
showAuthError("");
if (!window.__SUPABASE_CLIENT__) { showAuthError("Create account not configured."); return; }
var name = document.getElementById("auth-name-register").value.trim();
var email = document.getElementById("auth-email-register").value.trim();
var password = document.getElementById("auth-password-register").value;
try {
var res = await window.__SUPABASE_CLIENT__.auth.signUp({ email: email, password: password, options: { data: { name: name || email } } });
if (res.error) throw new Error(res.error.message);
if (res.data.session) {
setAppUserFromSession(res.data.session);
closeModal();
showAuthError("");
renderAuthUI();
if (typeof window.__onHfAuthChange === "function") window.__onHfAuthChange();
} else {
showAuthError("Account created. Please check your email to confirm, then sign in.");
}
} catch (err) { showAuthError(err.message || "Create account failed."); }
};
}
}
init();
})();
|