How can display the page according to the user role

Dear Boss
Please review my code below, which successfully retrieves the user ID and password from the user and displays the page. However, I have incorporated additional code to enable access for other users, yet it has not yielded the expected outcomes. I would like my code to display the page according to the user role. I would be grateful if you could provide the appropriate code to ensure that all users can view and access their assigned pages.

import streamlit as st
username =''
password = ''
if 'logged_in' not in st.session_state:
  st.session_state['logged_in'] = False

def login_page():
  st.title('Login')
  username = st.text_input('Username')
  password = st.text_input('Password', type='password')
  if st.button('Login'):
      if username == 'u' and password == 'p':
          st.session_state['logged_in'] = True
          st.header('Muhammad is the best')
          st.success('Logged in successfully!')
          st.rerun()
      elif username == 'o' and password == 'p':
          st.session_state['logged_in'] = True
          st.header('Muhammad is the best')
          st.success('Logged in successfully!')
          st.rerun()
      else:
          st.error('Invalid credentials.')

def dashboard_page():
  st.title('Dashboard')
  st.write('Welcome to your dashboard!')

def dashboard_page111():
  st.title('Tesing page  d')
  st.write('testing page =============!')

def admin_page():
  st.title('Admin Panel')
  st.write('Admin-only content.')

pages = [
st.Page(login_page, title='Login'),
]

if st.session_state['logged_in']:
  if username == 'u' and password == 'p':
      pages.append(st.Page(dashboard_page, title='Dashboard'))
  elif username == 'o' and password == 'p':
      pages.append(st.Page(dashboard_page111, title='Dashboard'))
# Conditionally add admin page
  if st.session_state.get('is_admin'): # Assuming you set this after login
      pages.append(st.Page(admin_page, title='Admin'))


current_page = st.navigation(pages)
current_page.run()


1 Like

Hi @badar

You can try this function based code and adapt it for your use:

import streamlit as st
from streamlit import session_state as ss

if 'logged_in' not in ss: ss['logged_in'] = False
def login_page():
  st.title('Login')
  username = st.text_input('Username', value='')
  password = st.text_input('Password', value='', type='password')
  if st.button('Login'):
      if username == 'u' and password == 'p':
        ss['logged_in'] = True
        ss.runpage = dashboard_page
        st.rerun()

      elif username == 'o' and password == 'p':
        ss['logged_in'] = True
        ss.runpage = dashboard_page111
        st.rerun()
      
      elif username == 'a' and password == 'p':
        ss.runpage = admin_page
        st.rerun()
      
      else: st.error('Invalid credentials.')

def dashboard_page():
  st.title('Dashboard')
  st.write('Welcome to your dashboard!')

  if st.button('Go Back'):
    ss.runpage = login_page
    st.rerun()

def dashboard_page111():
  st.title('Tesing page  d')
  st.write('testing page =============!')

  if st.button('Go Back'):
    ss.runpage = login_page
    st.rerun()

def admin_page():
  st.title('Admin Panel')
  st.write('Admin-only content.')

  if st.button('Go Back'):
    ss.runpage = login_page
    st.rerun()

if 'runpage' not in ss:  ss.runpage = login_page
ss.runpage()

Check the Streamlit docs for further reference on the commands / statements, in case you are not aware of them.

Cheers

1 Like