mkhorasani / Streamlit-Authenticator

A secure authentication module to validate user credentials in a Streamlit application.
Apache License 2.0
1.37k stars 229 forks source link

Logging out after resetting password #144

Closed vkyprmr closed 3 months ago

vkyprmr commented 3 months ago

I have the following code:


# Authenticating the user
authenticator = stauth.Authenticate(
    credentials['credentials'],
    credentials['cookie']['name'],
    credentials['cookie']['key'],
    credentials['cookie']['expiry_days'],
    credentials['preauthorized']
)
time.sleep(0.2)

# Layout
_, auth_col, _ = st.columns([1, 3, 1])

with auth_col:
    authenticator.login()
    username = st.session_state["username"]

if st.session_state["authentication_status"]:
    if st.session_state["username"] in credentials["advanced_users"]:
        default_extra.main()
    else:
        default_home.main()

    with st.sidebar:
        st.success(f"Logged in as: **{st.session_state['name']}**",
                    icon="✔")
        # Logout
        authenticator.logout()

        # Reset password
        if authenticator.reset_password(username):
            new_pwd = authenticator.credentials["usernames"][username]["password"]
            with open("streamlit-app/st-cache/credentials.yaml", "w",
                      encoding="utf-8") as cf:
                yaml.dump(credentials, cf)

            st.success("Password successfully reset", icon="✔")

elif st.session_state["authentication_status"] is None:
    auth_col.warning("*Please log in to access the aüpp.*", icon="‼")

else:
    auth_col.error("*Incorrect username or password. Please try again.*", icon="❌")

On reset_pasword, the user does not get logged out. Can I have it such that the user gets logged out and has to log in again? If it is supposed to happen, am I doing something wrong?

mkhorasani commented 3 months ago

Dear @vkyprmr what you can do to implement this workflow is to modify your code as follows:

        # Reset password
        if authenticator.reset_password(username):
            new_pwd = authenticator.credentials["usernames"][username]["password"]
            with open("streamlit-app/st-cache/credentials.yaml", "w",
                      encoding="utf-8") as cf:
                yaml.dump(credentials, cf)

            st.success("Password successfully reset", icon="✔")
            authenticator.logout(location='unrendered')
vkyprmr commented 3 months ago

Thanks for the update👍🏽