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…

6 Likes

i also encountered this question. have you already solved it?

1 Like

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

1 Like

Hi, I’m using this solution. But i found an issue if you reload the page in another page that doesn’t have the st.session_statue.authentication_status initialization it shows an error because it doesn’t exist.

Also is there any way to hide the options in the multi page menu in the case it isn’t initialized? or to redirect to the home and go back?

Thanks in advance

1 Like

Did anyone find a solution for this problem? I am facing it right now as well…

1 Like
if not st.session_state.get('authentication_status', False):
    st.info('Please Login from the Home page and try again.')
    st.stop()
2 Likes

I make a sample streamlit app dedicated for multi-page using the streamlit-authenticator library that can be used as a template or just for learning. It has login page, etc and can survive page reloads - meaning the user is re-logged in automatically. It also supports a user role, admin or not, and page are shown depending on the role of the user who logged in.

4 Likes

There’s also a demo app from @blackary showung how to create a multipage app with authentication

Demo app at:

Hope that helps!

1 Like

wish this demo could help: