Authentication store username in a variable to be used later

hello all!

so I’m following the code in this docs Authentication without SSO and its working very good, what I would like its that in the main app create some “Greetings (username)” but I was unable to store the username written on the login app to be used here.

this is the code that I’m using I was trying to use something like this but is not working on the main app

uservariable = st.session_state[“username”]

# streamlit_app.py

import streamlit as st

def check_password():
    """Returns `True` if the user had a correct password."""

    def password_entered():
        """Checks whether a password entered by the user is correct."""
        if (
            st.session_state["username"] in st.secrets["passwords"]
            and st.session_state["password"]
            == st.secrets["passwords"][st.session_state["username"]]
        ):
            st.session_state["password_correct"] = True
            del st.session_state["password"]  # don't store username + password
            del st.session_state["username"]
        else:
            st.session_state["password_correct"] = False

    if "password_correct" not in st.session_state:
        # First run, show inputs for username + password.
        st.text_input("Username", on_change=password_entered, key="username")
        st.text_input(
            "Password", type="password", on_change=password_entered, key="password"
        )
        return False
    elif not st.session_state["password_correct"]:
        # Password not correct, show input + error.
        st.text_input("Username", on_change=password_entered, key="username")
        st.text_input(
            "Password", type="password", on_change=password_entered, key="password"
        )
        st.error("😕 User not known or password incorrect")
        return False
    else:
        # Password correct.
        return True

if check_password():
    st.write("Here goes your normal Streamlit app...")
    st.button("Click me")

thanks!

Hi @Sbg,

Thanks for posting! Can you share some additional details to help us troubleshoot? What do you mean by “is not working on the main app”? Please share the full text of any error messages you’re seeing and any screenshots that might help our community members understand the issue.

Caroline :balloon:

I was unable to store the username written on the login app to be used here.

In order to have the username stored just remove the line where you are deleting it

del st.session_state["username"]