Spaces:
Configuration error
Configuration error
Download streamlit_app/utils/auth.py from rockysreenivasa/MathTutor: direct link, hf CLI and curl.
- Browser
- Download file 2.95 kB
-
https://huggingface.co/spaces/rockysreenivasa/MathTutor/resolve/main/streamlit_app/utils/auth.py
- Command line
-
hf download hf://spaces/rockysreenivasa/MathTutor/streamlit_app/utils/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/rockysreenivasa/MathTutor/resolve/main/streamlit_app/utils/auth.py
2.95 kB
| """ | |
| Supabase Auth helpers for Streamlit. | |
| Stores JWT and user info in st.session_state. | |
| """ | |
| import streamlit as st | |
| from supabase import Client, create_client | |
| import os | |
| from dotenv import load_dotenv | |
| _dotenv_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".env")) | |
| load_dotenv(dotenv_path=_dotenv_path) | |
| SUPABASE_URL = os.getenv("SUPABASE_URL", "") | |
| SUPABASE_ANON_KEY = os.getenv("SUPABASE_ANON_KEY", "") | |
| def get_supabase() -> Client: | |
| if not SUPABASE_URL or not SUPABASE_ANON_KEY: | |
| raise RuntimeError( | |
| "Missing Supabase env vars. Ensure SUPABASE_URL and SUPABASE_ANON_KEY are set in .env." | |
| ) | |
| return create_client(SUPABASE_URL, SUPABASE_ANON_KEY) | |
| def login(email: str, password: str) -> tuple[bool, str]: | |
| """Sign in with email/password. Returns (success, error_message).""" | |
| try: | |
| res = get_supabase().auth.sign_in_with_password( | |
| {"email": email, "password": password} | |
| ) | |
| st.session_state["access_token"] = res.session.access_token | |
| st.session_state["user_id"] = res.user.id | |
| st.session_state["user_email"] = res.user.email | |
| return True, "" | |
| except Exception as e: | |
| return False, str(e) | |
| def signup(email: str, password: str) -> tuple[bool, str]: | |
| """Sign up with email/password. Returns (success, message_or_error).""" | |
| try: | |
| res = get_supabase().auth.sign_up( | |
| {"email": email, "password": password} | |
| ) | |
| if not getattr(res, "user", None): | |
| return False, "Signup failed. Please try again." | |
| # If email confirmations are ON, Supabase creates the user but returns no session. | |
| session = getattr(res, "session", None) | |
| if session and getattr(session, "access_token", None): | |
| st.session_state["access_token"] = session.access_token | |
| st.session_state["user_id"] = res.user.id | |
| st.session_state["user_email"] = res.user.email | |
| return True, "" | |
| return True, ( | |
| "Account created. Please check your email to confirm your address, " | |
| "then come back and log in." | |
| ) | |
| except Exception as e: | |
| return False, str(e) | |
| def logout(): | |
| """Clear session state and sign out.""" | |
| try: | |
| get_supabase().auth.sign_out() | |
| except Exception: | |
| pass | |
| for key in ["access_token", "user_id", "user_email", "grade"]: | |
| st.session_state.pop(key, None) | |
| def is_logged_in() -> bool: | |
| return bool(st.session_state.get("access_token")) | |
| def require_login(): | |
| """Redirect to login page if not authenticated.""" | |
| if not is_logged_in(): | |
| st.warning("Please log in to continue.") | |
| st.page_link("pages/1_Login.py", label="Go to Login →") | |
| st.stop() | |
| def get_token() -> str: | |
| return st.session_state.get("access_token", "") | |
| def get_user_id() -> str: | |
| return st.session_state.get("user_id", "") | |