slyte / streamlit_app /auth.py
dashgaming's picture
Upload folder using huggingface_hub
f26cf5c verified
Raw History Blame Contribute Delete
1.65 kB
import streamlit as st
from firebase_admin import auth
def verify_user_from_query():
"""
Retrieves Firebase ID token from URL parameter 'token',
verifies it with Firebase Admin SDK, and stores UID in session state.
Returns:
uid (str): The verified User ID.
If token is missing or invalid, stops execution.
"""
# 1. Get token from URL query parameters
# Note: query_params might return a list if multiple tokens are present, usually not the case.
# In newer Streamlit versions, st.query_params acts like a dict.
# We'll use .get() which returns the value string or None.
print(f"DEBUG: ALL Query Params: {st.query_params}")
token = st.query_params.get("token")
print(f"DEBUG: Retrieved Token: {token}")
if not token:
st.warning("Please login first.")
# Stop execution so the rest of the app doesn't run
st.stop()
try:
# 2. Verify the ID token
decoded_token = auth.verify_id_token(token)
uid = decoded_token['uid']
# 3. Store UID in session state
st.session_state["uid"] = uid
return uid
except auth.ExpiredIdTokenError:
st.error("Session expired. Please login again.")
st.stop()
except auth.InvalidIdTokenError:
st.error("Invalid token. Please login again.")
st.stop()
except Exception as e:
# Catch other errors (e.g., network issues, malformed token)
st.error(f"Authentication error: {str(e)}")
st.stop()