mkhorasani / Streamlit-Authenticator

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

how to keep login after page refresh? #101

Closed Lix1993 closed 7 months ago

Lix1993 commented 7 months ago

Following are my codes, How can I keep page login after page refresh?

auth.py

import streamlit as st
import streamlit_authenticator as stauth
from data_tools import *

engine, conn, cur = create_engine(89, 9999, 'product__human_review')

def get_users():
    data = pd.read_sql('select * from web_user', engine)
    users = {}
    for i, row in data.iterrows():
        users[row['username']] = {'email': row['email'],
                                  'name': row['name'], 'password': row['password']}

    return users

users = get_users()

authenticator = stauth.Authenticate(
    credentials={
        'usernames': users
    },
    cookie_name='streamlit_auth',
    key='streamlit_auth_signature_key',
    cookie_expiry_days=30,
    preauthorized=None,
    validator=None,
)

def add_auth(func):

    def auth_func():

        if "authentication_status" not in st.session_state:
            st.session_state["authentication_status"] = None
            st.session_state["username"] = None
            st.session_state["name"] = None

        name, authentication_status, username = authenticator.login(
            'Login', 'main')

        if st.session_state["authentication_status"]:
            func()
            st.sidebar.text(f'User: {st.session_state["username"]}')
            authenticator.logout('Logout', 'sidebar')

        elif st.session_state["authentication_status"] == False:
            st.error('Username/password is incorrect')
        elif st.session_state["authentication_status"] == None:
            st.warning('Please enter your username and password')

    return auth_func
import streamlit as st

from product_data_parser.WebComponent.auth import add_auth

@add_auth
def main():
    st.title('Title')
    st.text('content')

main()
mkhorasani commented 7 months ago

Hi @Lix1993, I'm not sure if I understood you correctly, but once you log in the login widget will disappear. There is currently no way to keep the widget post-login.

Lix1993 commented 7 months ago

Hi @mkhorasani,

Thank you for your response. I think there might have been a misunderstanding regarding my issue. My concern is not about the login widget's visibility post-login, but rather about maintaining the user's logged-in status even after the webpage is refreshed.

Currently, in my Streamlit application, whenever the page is refreshed, the session state seems to reset, and the user is required to log in again. My goal is to have a persistent login state across page refreshes, so the user does not need to re-enter their credentials every time the page reloads.

I am using Streamlit Authenticator and have set up a cookie named 'streamlit_auth'. However, this doesn't seem to maintain the login state after a page refresh. Is there a specific configuration or approach within Streamlit Authenticator that I should follow to achieve persistent login sessions across page refreshes?

Any guidance or suggestions on how to resolve this would be greatly appreciated.

Generated from Chatgpt.

litterGuy commented 5 months ago

I have the same problem, how to solve it?

Grepta commented 5 months ago

I also seem to be encountering the same issue. When running it locally, a cookie saves to the browser just fine. However, once deployed to the cloud a cookie isn't being saved to the browser.

mkhorasani commented 3 months ago

Dear all, if you are using Streamlit-Authenticator with multi-page apps, you will have to recreate the authenticator object on each and every page and invoke the login method as shown below:

authenticator = stauth.Authenticate(
    config['credentials'],
    config['cookie']['name'],
    config['cookie']['key'],
    config['cookie']['expiry_days'],
    config['pre-authorized']
)

authenticator.login()

This is to ensure that when a user hard refreshes the page and the session state variables related to re-authentication are lost, the authenticator object is there to re-initialize them from the cookie saved on the browser.

Lix1993 commented 3 months ago

thx, It works