Is it possible to use multipage app feature with user authentication with streamlit? Iāve made a multipage app with strealit-authenticator, but when i make an authentication for user in my āmain pageā the login page appear and protect only on the āmain pageā, other pages can access on the sidebar without having to login.
Please help! Thank youā¦
5 Likes
i also encountered this question. have you already solved it?
This can be soived using st.session_state in Streamlit. You can setup the main file like:
import streamlit as st
import streamlit_authenticator as stauth
import yaml
from yaml.loader import SafeLoader
st.set_page_config(page_title="Home", page_icon="š ", layout="wide")
def st_authenticator():
with open('data/config.yaml') as file:
config = yaml.load(file, Loader=SafeLoader)
file.close()
authenticator = stauth.Authenticate(
config['credentials'],
config['cookie']['name'],
config['cookie']['key'],
config['cookie']['expiry_days'],
config['preauthorized']
)
return authenticator
authenticator = st_authenticator()
name, authentication_status, username = authenticator.login("Login", "main")
if authentication_status:
st.session_state.authentication_status = True
authenticator.logout('**Logout**', 'main', key='unique_key')
elif authentication_status is False:
st.session_state.authentication_status = False
st.error('Username/password is incorrect')
elif authentication_status is None:
st.session_state.authentication_status = None
st.warning('Please enter your username and password')
And include the below line of code in other pages (after importing libraries). For example in āpages/1_FirstPage.pyā
# import libraries here
if not st.session_state.authentication_status:
st.info('Please Login from the Home page and try again.')
st.stop()
# rest of the operations