Spaces:
Sleeping
Sleeping
Commit
·
8e17e8f
1
Parent(s):
6d20fb8
Improve OAuth login button visibility and error handling
Browse files- src/Start_Page.py +4 -1
- src/gui_elements/session_cache.py +30 -20
src/Start_Page.py
CHANGED
|
@@ -27,8 +27,11 @@ handle_oauth_callback()
|
|
| 27 |
login_required, login_error_message = require_hf_login()
|
| 28 |
if login_required:
|
| 29 |
st.error(login_error_message)
|
| 30 |
-
st.info("💡 **Tip:**
|
|
|
|
| 31 |
login_button() # Show the login button
|
|
|
|
|
|
|
| 32 |
st.stop() # Stop execution - user must log in
|
| 33 |
|
| 34 |
# User is logged in - show username (optional)
|
|
|
|
| 27 |
login_required, login_error_message = require_hf_login()
|
| 28 |
if login_required:
|
| 29 |
st.error(login_error_message)
|
| 30 |
+
st.info("💡 **Tip:** Click the button below to sign in with your Hugging Face account.")
|
| 31 |
+
st.divider()
|
| 32 |
login_button() # Show the login button
|
| 33 |
+
st.divider()
|
| 34 |
+
st.info("**Note:** You need to complete the OAuth flow within this app, even if you're already logged into Hugging Face in your browser.")
|
| 35 |
st.stop() # Stop execution - user must log in
|
| 36 |
|
| 37 |
# User is logged in - show username (optional)
|
src/gui_elements/session_cache.py
CHANGED
|
@@ -34,30 +34,40 @@ def login_button():
|
|
| 34 |
"""Display a login button that redirects to HF OAuth."""
|
| 35 |
if not os.environ.get("OAUTH_CLIENT_ID"):
|
| 36 |
st.error("OAuth is not configured. Please enable hf_oauth: true in README.md")
|
|
|
|
| 37 |
return
|
| 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 |
def handle_oauth_callback():
|
| 63 |
"""Handle OAuth callback and exchange code for token."""
|
|
|
|
| 34 |
"""Display a login button that redirects to HF OAuth."""
|
| 35 |
if not os.environ.get("OAUTH_CLIENT_ID"):
|
| 36 |
st.error("OAuth is not configured. Please enable hf_oauth: true in README.md")
|
| 37 |
+
st.info("The Space needs to be rebuilt after adding hf_oauth: true to README.md")
|
| 38 |
return
|
| 39 |
|
| 40 |
+
try:
|
| 41 |
+
cfg = oidc_config()
|
| 42 |
+
if not cfg:
|
| 43 |
+
st.error("Could not fetch OpenID configuration. Please try again later.")
|
| 44 |
+
return
|
| 45 |
+
|
| 46 |
+
auth_endpoint = cfg.get("authorization_endpoint")
|
| 47 |
+
if not auth_endpoint:
|
| 48 |
+
st.error("Could not get authorization endpoint from OpenID configuration.")
|
| 49 |
+
return
|
| 50 |
|
| 51 |
+
state = secrets.token_urlsafe(24)
|
| 52 |
+
st.session_state["oauth_state"] = state
|
| 53 |
|
| 54 |
+
redirect_uri = get_space_url() + "/"
|
| 55 |
+
if not redirect_uri:
|
| 56 |
+
st.warning("Could not determine Space URL. OAuth may not work correctly.")
|
| 57 |
+
redirect_uri = "https://" + os.getenv("SPACE_HOST", "") + "/"
|
| 58 |
+
|
| 59 |
+
params = {
|
| 60 |
+
"client_id": os.environ["OAUTH_CLIENT_ID"],
|
| 61 |
+
"redirect_uri": redirect_uri,
|
| 62 |
+
"response_type": "code",
|
| 63 |
+
"scope": os.getenv("OAUTH_SCOPES", "openid profile"),
|
| 64 |
+
"state": state,
|
| 65 |
+
}
|
| 66 |
+
url = auth_endpoint + "?" + urllib.parse.urlencode(params)
|
| 67 |
+
st.link_button("🔐 Sign in with Hugging Face", url, use_container_width=True)
|
| 68 |
+
except Exception as e:
|
| 69 |
+
st.error(f"Error setting up OAuth login: {e}")
|
| 70 |
+
st.info("Please check the Space logs for more details.")
|
| 71 |
|
| 72 |
def handle_oauth_callback():
|
| 73 |
"""Handle OAuth callback and exchange code for token."""
|