Spaces:
Paused
Paused
Sign-in via capture-phase HTML buttons (bypass Gradio SPA)
Browse filesGradio Button/LoginButton click JS never fired on this Space even with
correct root. Plain HTML buttons + document capture listener assign
window.top.location to /login/huggingface. Hidden LoginButton kept so
OAuth routes mount.
Files changed (2)
- app.py+65-28
- tests/test_auth_status.py+6-5
47 unmodified lines4849505152535455565758596061626364656667823 unmodified lines89189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192247 unmodified lines)# Gradio's SPA client intercepts in-app <a href="/login/..."> clicks (no# navigation). Drive OAuth via window.top.location instead. A hidden# LoginButton is still required so Gradio mounts /login/huggingface# (blocks.expects_oauth).LOGIN_JS = ("() => { const u = window.location.origin + ""'/login/huggingface?_target_url=/'; ""window.top.location.assign(u); }")LOGOUT_JS = ("() => { const u = window.location.origin + ""'/logout?_target_url=/'; ""window.top.location.assign(u); }")import numpy as npimport runpod_i2v823 unmodified lines.big-btn button { font-size: 1.15rem !important; min-height: 3rem !important; }#status-md { font-size: 1rem; }#auth-status-md { font-size: 0.95rem; }.hf-auth-row { margin: 0.35rem 0 0.75rem; gap: 0.5rem; }footer { display: none !important; }"""def build_ui():runpod = runpod_i2v.active_backend() == "runpod"with gr.Blocks(title="Wan 2.2 Extend — I2V", css=CSS, theme=gr.themes.Soft()) as demo:gr.Markdown(intro_markdown())auth_status = gr.Markdown("Checking sign-in…",elem_id="auth-status-md",)# Mounts OAuth routes (/login/huggingface). Hidden — its own click JS# was unreliable on Spaces; visible buttons below drive navigation.gr.LoginButton(visible=False)with gr.Row(elem_classes=["hf-auth-row"]):btn_login = gr.Button("Sign in with Hugging Face",variant="huggingface",size="lg",scale=2,)btn_logout = gr.Button("Logout", variant="secondary", size="lg", scale=1)btn_login.click(fn=None, inputs=None, outputs=None, js=LOGIN_JS)btn_logout.click(fn=None, inputs=None, outputs=None, js=LOGOUT_JS)gr.Markdown(f"Sign-in opens HF OAuth in the **top window** (iframe-safe). "f"Owner-only: `{OWNER_USERNAME}`."47 unmodified lines4849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697823 unmodified lines92192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895947 unmodified lines)# Gradio's client often swallows in-app <a>/Button clicks (SPA router /# broken JS deps on Spaces). Use a capture-phase document listener in# Blocks(head=...) plus plain HTML buttons. Hidden LoginButton is still# required so Gradio mounts /login/huggingface (blocks.expects_oauth).AUTH_HEAD_JS = """<script>(function () {function go(path) {try {var url = window.location.origin + path;(window.top || window).location.assign(url);} catch (e) {window.location.assign(window.location.origin + path);}}document.addEventListener("click",function (ev) {var t = ev.target && ev.target.closest? ev.target.closest("[data-hf-oauth]"): null;if (!t) return;ev.preventDefault();ev.stopPropagation();var kind = t.getAttribute("data-hf-oauth");if (kind === "login") go("/login/huggingface?_target_url=/");else if (kind === "logout") go("/logout?_target_url=/");},true);})();</script>"""AUTH_BUTTONS_HTML = """<div class="hf-auth-bar"><button type="button" class="hf-auth-btn hf-auth-login" data-hf-oauth="login">Sign in with Hugging Face</button><button type="button" class="hf-auth-btn hf-auth-logout" data-hf-oauth="logout">Logout</button></div>"""import numpy as npimport runpod_i2v823 unmodified lines.big-btn button { font-size: 1.15rem !important; min-height: 3rem !important; }#status-md { font-size: 1rem; }#auth-status-md { font-size: 0.95rem; }.hf-auth-bar { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; margin: 0.4rem 0 0.75rem; }.hf-auth-btn {cursor: pointer;padding: 0.7rem 1.15rem;border-radius: 8px;font-size: 1rem;font-weight: 600;border: 1px solid #333;color: #fff;background: #0b0f19;}.hf-auth-btn:hover { filter: brightness(1.12); }.hf-auth-logout { background: #3f3f46; font-weight: 500; }footer { display: none !important; }"""def build_ui():runpod = runpod_i2v.active_backend() == "runpod"with gr.Blocks(title="Wan 2.2 Extend — I2V",css=CSS,theme=gr.themes.Soft(),head=AUTH_HEAD_JS,) as demo:gr.Markdown(intro_markdown())auth_status = gr.Markdown("Checking sign-in…",elem_id="auth-status-md",)# Mounts OAuth routes (/login/huggingface). Must exist for expects_oauth.gr.LoginButton(visible=False)gr.HTML(AUTH_BUTTONS_HTML)gr.Markdown(f"Sign-in opens HF OAuth in the **top window** (iframe-safe). "f"Owner-only: `{OWNER_USERNAME}`."
88 unmodified lines899091929394959697989988 unmodified linesother = app.auth_status_markdown(_Profile("other"))self.assertIn("owner-only", other)def test_login_js_uses_top_window(self):self.assertIn("/login/huggingface", app.LOGIN_JS)self.assertIn("window.top.location", app.LOGIN_JS)self.assertIn("/logout", app.LOGOUT_JS)self.assertIn("window.top.location", app.LOGOUT_JS)if __name__ == "__main__":88 unmodified lines899091929394959697989910088 unmodified linesother = app.auth_status_markdown(_Profile("other"))self.assertIn("owner-only", other)def test_auth_head_js_capture_listener(self):self.assertIn("data-hf-oauth", app.AUTH_HEAD_JS)self.assertIn("/login/huggingface", app.AUTH_HEAD_JS)self.assertIn("addEventListener", app.AUTH_HEAD_JS)self.assertIn('data-hf-oauth="login"', app.AUTH_BUTTONS_HTML)self.assertIn('data-hf-oauth="logout"', app.AUTH_BUTTONS_HTML)if __name__ == "__main__":
app.py+65-28
tests/test_auth_status.py+6-5