Please take a moment to search the forum and documentation before posting a new topic.
If you’re creating a debugging post, please include the following info:
- Are you running your app locally or is it deployed? Yes deployed.
- If your app is deployed:
a. Is it deployed on Community Cloud or another hosting platform? AWS EC2. - Share the Streamlit and Python versions: streamlit~=1.43.1
I’m using API and cookie manager to handle the session. I’m not using Streamlit-authenticator. My issue is application taking me to the Login page when we do login and refresh the page. Below is the logic used:
def get_cookie_manager():
return stx.CookieManager(key=“cookie_manager”)
cookie_manager = get_cookie_manager()
def authentication():
“”"
Authenticate user and manage session state.
“”"
# Generate a unique session ID if not already present
if “browser_session_id” not in st.session_state:
st.session_state[“browser_session_id”] = str(uuid.uuid4())
# Initialize basic session state variables
if "authentication_status" not in st.session_state:
st.session_state["authentication_status"] = None
if "active_page" not in st.session_state:
st.session_state["active_page"] = "login"
if "role" not in st.session_state:
st.session_state["role"] = ""
# Check if user is authenticated by cookie and session ID verification
if "token" not in st.session_state:
token = cookie_manager.get("user_token")
browser_id = cookie_manager.get("browser_session_id")
# Only restore session if the browser session ID matches
if token and browser_id and browser_id == st.session_state["browser_session_id"]:
st.session_state["authentication_status"] = True
st.session_state["token"] = token
st.session_state["name"] = cookie_manager.get("user_name") or "User"
st.session_state["role"] = cookie_manager.get("user_role") or ""
st.session_state["email"] = cookie_manager.get("user_email") or ""
st.session_state["active_page"] = "main"
# Force UI refresh after restoring session
st.rerun()
# Handle authentication and logout
if st.session_state["authentication_status"]:
st.sidebar.write(f"👤 Welcome *{st.session_state.get('name', 'User')}*")
if st.sidebar.button("Logout"):
token = st.session_state.get("token")
if token:
response = logout_user(token=token)
if response.status_code == 200:
st.session_state.clear() # Clear all session data
delete_cookies()
# Reset session state variables after logout
st.session_state["authentication_status"] = False
st.session_state["role"] = ""
st.session_state["token"] = None
st.session_state["name"] = ""
st.session_state["email"] = ""
st.session_state["active_page"] = "login"
# Generate a new browser session ID
st.session_state["browser_session_id"] = str(uuid.uuid4())
st.rerun()
# Handle role-based project selection (for non-admins)
if st.session_state["role"] != "admin":
projects = get_user_projects(st.session_state["email"], st.session_state["token"])
if not isinstance(projects, list):
st.warning("You do not have any projects assigned. Please contact your admin.")
return False
st.session_state["projects"] = {proj["project_id"]: proj["project_name"] for proj in projects}
if st.session_state["projects"]:
project_options = list(st.session_state["projects"].values())
selected_project = st.sidebar.selectbox("Choose a Project", project_options)
selected_project_id = next(
(pid for pid, pname in st.session_state["projects"].items() if pname == selected_project), None
)
if selected_project_id is not None:
st.session_state["selected_project_id"] = selected_project_id
else:
st.sidebar.error("Invalid project selection.")
else:
st.sidebar.error("No projects assigned. Contact your admin.")
return True
# Handle login/register form display
else:
if st.session_state["active_page"] == "login":
login_form()
elif st.session_state["active_page"] == "register":
create_account_form()
return False
def delete_cookies():
“”"
Helper function to safely delete cookies and reset necessary session state values.
“”"
# Get all current cookies
cookies = cookie_manager.get_all()
# Only delete cookies if they exist
if "user_token" in cookies:
cookie_manager.delete("user_token", key="delete_token")
if "user_name" in cookies:
cookie_manager.delete("user_name", key="delete_name")
if "user_role" in cookies:
cookie_manager.delete("user_role", key="delete_role")
if "user_email" in cookies:
cookie_manager.delete("user_email", key="delete_email")
if "browser_session_id" in cookies:
cookie_manager.delete("browser_session_id", key="delete_session_id")
# Reset session state values
st.session_state["authentication_status"] = False
st.session_state["role"] = ""
st.session_state["token"] = None
st.session_state["name"] = ""
st.session_state["email"] = ""
st.session_state.active_page = "login"