How to manage a multipage app with user authentication

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