Just close the sidebar and hide the open button using style until auth then re-run the page to load them.
Use something like this…
import time
import streamlit as st
st.set_page_config(initial_sidebar_state="collapsed")
if "user_authorised" not in st.session_state:
st.session_state['user_authorised'] = False
# load cookies for auth status. If exists save to st.session_state['user_authorised']. something like this...
try:
auth_cookies = .... #load cookies that has saved auth status here. If it exists it will save else the next code bloack will load.
st.session_state['user_authorised'] = auth_cookies['status']
except:
st.session_state['user_authorised'] = False #if it does not exist, it will just go back to its default which is False - suggests new user or perhaps cookies expired.
if st.session_state['user_authorised'] == False:
st.markdown(
f"""
<style>
[data-testid="collapsedControl"] {{
display: none;
}}
</style>
""",
unsafe_allow_html=True,
)
# input auth code here to authorise user.
user_auth_status = ....
# Once authorised change session state to True then re-run code
if user_auth_status == True:
st.session_state['user_authorised'] = True
# save status to cookies or database
....
time.sleep(1)
st.experiemental_rerun()
On rerun, is the code block inside st.session_state[‘user_authorised’] will not be run and user will see sidebar and pages. But make sure you save the user auth status in some database or in cookies and load it at the beginning of the page and save to session_state[‘user_authorised’] because if browser closes session_state will be reset back to False and user will have to re-login again - bad for user experience.