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"]

@Goyo your solution indeed makes sense but it is not working for me. I am using the same piece of code shown by @Sbg . I tried many things. Even using global variable. But nothing is working. Once we are outside the login page, it seems st.session_state["username"] just disappears. Which is weird because that is exactly why session_state was built so I am very confused.

My solution is not using the same piece of code but fixing it. It works for me after the fix.

I resolved the issue. Uncommenting del st.session_state["username"] is NOT enough if, like in my case, after the if check_password():, there is a lot going on and the entire script is re-run. The solution was to define a new session_state variable and use that for the rest of the app. See below:

if check_password():

if 'kept_username' not in st.session_state:
    st.session_state['kept_username'] = st.session_state['username']

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