Different authentication for different pages in multipage

hey famm!!
I have created a multipage app and I have set separate credentials for each page.But if I am logging in to one page and switching to another page from side bar I am able to see the contents of the other page which should not happen as I have set different credentials for each page.
Help me solve the issue.

Could you post a minimal reproducible code? Let’s take a look.

heyy @ferdy
heres the code that I am using:
auth.yaml file-

Pages:
   page1:
    credentials:
      usernames:
        admin:
          email: abc.1@gmail.com
          name: Admin
          password: hashed_password1
    cookie:
        expiry_days: 0
        key: some_key
        name: some_cookie
    preauthorized:
       emails:
       - melsby@gmail.com
  page2:
    credentials:
      usernames:
        admin1:
          email: abc.1@gmail.com
          name: Admin1
          password: hashed_paswword2
    cookie1:
      expiry_days: 0
      key: some_key1
      name: some_cookie1
    preauthorized:
     emails:
      - melsby@gmail.com

In main.py of the page:

  with open('auth.yaml') as file:
        config = yaml.load(file, Loader=SafeLoader)
    authenticator = stauth.Authenticate(
    config['Pages’][‘page2’][‘credentials'],
    config['Pages’][‘page2’][‘cookie1']['name'],
    config['Pages’][‘page2’][‘cookie1']['key'],
    config['Pages’][‘page2’][‘cookie1']['expiry_days'],
    config['Pages’][‘page2’][‘preauthorized'])
    # st.write( config)
    # Attempt login
    name, authentication_status, username = authenticator.login()
 
    if authentication_status :
         #contents of the page are displayed along with a logout button
          authenticator.logout('Logout', 'main', key='unique_key')
    elif authentication_status is False:
        st.error('Username/password is incorrect')
    elif authentication_status is None:
        st.warning('Please enter your username and password')

i have created a auth file for my credentials and then I am fetching the credentials of each page from the same file.

This is not how it works, the yaml file is only used by the authenticator.

To solve your issue, you have to check which name or username has logged in. And depending on the permission or role, you have to program which page to show.

I have tried it that way also.So if I am adding the username check and switch to the other page the page is being blank instead of showing the login page

I make a sample app on how to handle streamlit multipage app with roles and streamlit-authenticator lib. It can handle page refresh from different pages.

In the beginning, we have the login page.

After log-in we add two more pages if user is an admin, if not just show one additional page.

jsmith is an admin.

ROLES = {'jsmith': 'admin', 'rbriggs': 'user'}

def MenuButtons():
    if 'authentication_status' not in ss:
        ss.authentication_status = False

    # Always show the home and login navigators.
    HomeNav()
    LoginNav()

    # Show the other page navigators depending on the users' role.
    if ss["authentication_status"]:

        # (1) Only the admin role can access page 1 and other pages.
        # In a pre-defined ROLES, get all the usernames with admin role.
        admins = [k for k, v in ROLES.items() if v == 'admin']

        # Show page 1 if the username that logged in is an admin.
        if ss.username in admins:
            Page1Nav()

        # (2) roles with users and admin have access to page 2.
        Page2Nav()     

image

The app code is in my github repo.

username/password

jsmith / abc
rbriggs / def
1 Like