Simzy commited on
Commit
83a2ebc
·
unverified ·
1 Parent(s): 72af5ae

Sign-in via capture-phase HTML buttons (bypass Gradio SPA)

Browse files

Gradio 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) hide show
  1. app.py +65 -28
  2. tests/test_auth_status.py +6 -5
app.py CHANGED
@@ -48,20 +48,50 @@ def auth_status_markdown(profile: gr.OAuthProfile | None = None) -> str:
48
  )
49
 
50
 
51
- # Gradio's SPA client intercepts in-app <a href="/login/..."> clicks (no
52
- # navigation). Drive OAuth via window.top.location instead. A hidden
53
- # LoginButton is still required so Gradio mounts /login/huggingface
54
- # (blocks.expects_oauth).
55
- LOGIN_JS = (
56
- "() => { const u = window.location.origin + "
57
- "'/login/huggingface?_target_url=/'; "
58
- "window.top.location.assign(u); }"
59
- )
60
- LOGOUT_JS = (
61
- "() => { const u = window.location.origin + "
62
- "'/logout?_target_url=/'; "
63
- "window.top.location.assign(u); }"
64
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  import numpy as np
67
  import runpod_i2v
@@ -891,32 +921,39 @@ CSS = """
891
  .big-btn button { font-size: 1.15rem !important; min-height: 3rem !important; }
892
  #status-md { font-size: 1rem; }
893
  #auth-status-md { font-size: 0.95rem; }
894
- .hf-auth-row { margin: 0.35rem 0 0.75rem; gap: 0.5rem; }
 
 
 
 
 
 
 
 
 
 
 
 
895
  footer { display: none !important; }
896
  """
897
 
898
 
899
  def build_ui():
900
  runpod = runpod_i2v.active_backend() == "runpod"
901
- with gr.Blocks(title="Wan 2.2 Extend — I2V", css=CSS, theme=gr.themes.Soft()) as demo:
 
 
 
 
 
902
  gr.Markdown(intro_markdown())
903
  auth_status = gr.Markdown(
904
  "Checking sign-in…",
905
  elem_id="auth-status-md",
906
  )
907
- # Mounts OAuth routes (/login/huggingface). Hidden — its own click JS
908
- # was unreliable on Spaces; visible buttons below drive navigation.
909
  gr.LoginButton(visible=False)
910
- with gr.Row(elem_classes=["hf-auth-row"]):
911
- btn_login = gr.Button(
912
- "Sign in with Hugging Face",
913
- variant="huggingface",
914
- size="lg",
915
- scale=2,
916
- )
917
- btn_logout = gr.Button("Logout", variant="secondary", size="lg", scale=1)
918
- btn_login.click(fn=None, inputs=None, outputs=None, js=LOGIN_JS)
919
- btn_logout.click(fn=None, inputs=None, outputs=None, js=LOGOUT_JS)
920
  gr.Markdown(
921
  f"Sign-in opens HF OAuth in the **top window** (iframe-safe). "
922
  f"Owner-only: `{OWNER_USERNAME}`."
 
48
  )
49
 
50
 
51
+ # Gradio's client often swallows in-app <a>/Button clicks (SPA router /
52
+ # broken JS deps on Spaces). Use a capture-phase document listener in
53
+ # Blocks(head=...) plus plain HTML buttons. Hidden LoginButton is still
54
+ # required so Gradio mounts /login/huggingface (blocks.expects_oauth).
55
+ AUTH_HEAD_JS = """
56
+ <script>
57
+ (function () {
58
+ function go(path) {
59
+ try {
60
+ var url = window.location.origin + path;
61
+ (window.top || window).location.assign(url);
62
+ } catch (e) {
63
+ window.location.assign(window.location.origin + path);
64
+ }
65
+ }
66
+ document.addEventListener(
67
+ "click",
68
+ function (ev) {
69
+ var t = ev.target && ev.target.closest
70
+ ? ev.target.closest("[data-hf-oauth]")
71
+ : null;
72
+ if (!t) return;
73
+ ev.preventDefault();
74
+ ev.stopPropagation();
75
+ var kind = t.getAttribute("data-hf-oauth");
76
+ if (kind === "login") go("/login/huggingface?_target_url=/");
77
+ else if (kind === "logout") go("/logout?_target_url=/");
78
+ },
79
+ true
80
+ );
81
+ })();
82
+ </script>
83
+ """
84
+
85
+ AUTH_BUTTONS_HTML = """
86
+ <div class="hf-auth-bar">
87
+ <button type="button" class="hf-auth-btn hf-auth-login" data-hf-oauth="login">
88
+ Sign in with Hugging Face
89
+ </button>
90
+ <button type="button" class="hf-auth-btn hf-auth-logout" data-hf-oauth="logout">
91
+ Logout
92
+ </button>
93
+ </div>
94
+ """
95
 
96
  import numpy as np
97
  import runpod_i2v
 
921
  .big-btn button { font-size: 1.15rem !important; min-height: 3rem !important; }
922
  #status-md { font-size: 1rem; }
923
  #auth-status-md { font-size: 0.95rem; }
924
+ .hf-auth-bar { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; margin: 0.4rem 0 0.75rem; }
925
+ .hf-auth-btn {
926
+ cursor: pointer;
927
+ padding: 0.7rem 1.15rem;
928
+ border-radius: 8px;
929
+ font-size: 1rem;
930
+ font-weight: 600;
931
+ border: 1px solid #333;
932
+ color: #fff;
933
+ background: #0b0f19;
934
+ }
935
+ .hf-auth-btn:hover { filter: brightness(1.12); }
936
+ .hf-auth-logout { background: #3f3f46; font-weight: 500; }
937
  footer { display: none !important; }
938
  """
939
 
940
 
941
  def build_ui():
942
  runpod = runpod_i2v.active_backend() == "runpod"
943
+ with gr.Blocks(
944
+ title="Wan 2.2 Extend — I2V",
945
+ css=CSS,
946
+ theme=gr.themes.Soft(),
947
+ head=AUTH_HEAD_JS,
948
+ ) as demo:
949
  gr.Markdown(intro_markdown())
950
  auth_status = gr.Markdown(
951
  "Checking sign-in…",
952
  elem_id="auth-status-md",
953
  )
954
+ # Mounts OAuth routes (/login/huggingface). Must exist for expects_oauth.
 
955
  gr.LoginButton(visible=False)
956
+ gr.HTML(AUTH_BUTTONS_HTML)
 
 
 
 
 
 
 
 
 
957
  gr.Markdown(
958
  f"Sign-in opens HF OAuth in the **top window** (iframe-safe). "
959
  f"Owner-only: `{OWNER_USERNAME}`."
tests/test_auth_status.py CHANGED
@@ -89,11 +89,12 @@ class AuthTests(unittest.TestCase):
89
  other = app.auth_status_markdown(_Profile("other"))
90
  self.assertIn("owner-only", other)
91
 
92
- def test_login_js_uses_top_window(self):
93
- self.assertIn("/login/huggingface", app.LOGIN_JS)
94
- self.assertIn("window.top.location", app.LOGIN_JS)
95
- self.assertIn("/logout", app.LOGOUT_JS)
96
- self.assertIn("window.top.location", app.LOGOUT_JS)
 
97
 
98
 
99
  if __name__ == "__main__":
 
89
  other = app.auth_status_markdown(_Profile("other"))
90
  self.assertIn("owner-only", other)
91
 
92
+ def test_auth_head_js_capture_listener(self):
93
+ self.assertIn("data-hf-oauth", app.AUTH_HEAD_JS)
94
+ self.assertIn("/login/huggingface", app.AUTH_HEAD_JS)
95
+ self.assertIn("addEventListener", app.AUTH_HEAD_JS)
96
+ self.assertIn('data-hf-oauth="login"', app.AUTH_BUTTONS_HTML)
97
+ self.assertIn('data-hf-oauth="logout"', app.AUTH_BUTTONS_HTML)
98
 
99
 
100
  if __name__ == "__main__":