Hiding pages during login - register

Summary

I tried to create a login register system for my streamlit app but I have hard time to hide pages and show them after registering or logining.
I tried solution here but didnโ€™t worked for me: example

Steps to reproduce

Code

import streamlit as st
import pymongo
import bcrypt
def get_database():
    client = pymongo.MongoClient()
    return client['db']


dbname = get_database()
users = dbname["users"]

def register(username, email, password):
    # Hash the password
    hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
    # Check if the username already exists in the users collection
    existing_user = users.find_one({"username": username})
    if existing_user:
        return False
    else:
        # Insert user data into the users collection
        users.insert_one({
            "username": username,
            "email": email,
            "password": hashed_password.decode()
        })
        return True

def login(username, password):
    # Check if the user exists in the users collection
    user = users.find_one({"username": username})
    if user:
        # Compare the entered password with the hashed password stored in the database
        if bcrypt.checkpw(password.encode('utf-8'), user["password"].encode('utf-8')):
            return True
    return False

def login_page():
    username = st.text_input("Username")
    email = st.text_input("Email")
    password = st.text_input("Password", type='password')
    global is_logged_in
    if st.button("Login"):
        if login(username, password):
            is_logged_in = True
            st.success("Logged in!")
        else:
            st.error("Invalid username or password")
    if st.button("Register"):
        if register(username, email, password):
            is_logged_in = True
            st.success("User registered!")
        else:
            st.error("Username already exists")

def navigation():
    st.sidebar.title("Navigation")
    is_logged_in = False
    if st.button("Home"):
        st.write("This is the home page")
    if is_logged_in:
        pages = ['page1','page2']
        for page in pages:
            if st.button(page.replace("_"," ").title()):
                mod = __import__(f"pages.{page}", fromlist=[page])
                func_name = f"{page}_page"
                func = getattr(mod, func_name)
                func()
        if st.button("Logout"):
            is_logged_in = False
            st.success("Logged out!")
    else:
        st.warning("Please login to access other pages.")

is_logged_in = False
if not is_logged_in:
    st.set_page_config(initial_sidebar_state="collapsed")
    login_page()
else:
    st.write("Welcome to my App")
    st.set_page_config(initial_sidebar_state="expanded")
    navigation()

I have pages file which include 2 different python file as page.
I can login and register there is no problem with that.

Expected behavior:
I wanted to pages been seen and used when login or register is successful only and also I wanted to add a logout button make user to return login or register page when pressed.

Actual behavior:
Even the login page opened I can still see pages in left side without actually login or register, I canโ€™t restrict them to be seen before login or register.

Debug info

  • Streamlit version: 1.17.0
  • Python version: 3.8
  • In Venv enviroment
  • OS version: mac
  • Browser version: safari

Requirements file

numpy==1.21.0
requests==2.26.0
pyOpenSSL==22.1.0
dnspython==2.0.0
pymongo==3.11.3
plotly == 5.12.0
protobuf==3.19.4
click==7.1.2
streamlit==1.17.0

This is now possible with the latest release (0.4.0) of st_pages st-pages ยท PyPI

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.