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

Code executed 3 times when login #33

Open kinokocorp opened 1 year ago

kinokocorp commented 1 year ago

Hello, When you log in with Streamlit-Authenticator, the code is executed 3 times. I added a print('test') command and I can see 'test' 3 times in the console.

I'm using version 0.2.1.

import streamlit as st
import streamlit_authenticator as stauth
import yaml

st.set_page_config(page_title='Auth', layout='wide')

# Load Authenticator Configuration
with open('config.yaml') as file:
    config = yaml.load(file, Loader=stauth.SafeLoader)

# Authenticator Parameters
authenticator = stauth.Authenticate(
    config['credentials'],
    config['cookie']['name'],
    config['cookie']['key'],
    config['cookie']['expiry_days']
)

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

if st.session_state["authentication_status"]:
    authenticator.logout('Logout', 'main')
    st.write(f'Welcome *{st.session_state["name"]}*')
    st.title('Some content')
    print("test")
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')

After logging in, the problem doesn't occur again.

mkhorasani commented 1 year ago

Hi @kinokocorp, I believe this is related to the extra_streamlit_components package that reads the reauthentication cookie. Thank you for pointing this out, as soon as a robust solution is found I will push a new release.

mvn-hoangnguyen-hn commented 8 months ago

Hi @mkhorasani, i am having similar problem with Streamlit-Authenticator, the code is executed multiple times. I am using extra-streamlit-components version 0.1.60, streamlit-authenticator version 0.2.3 and streamlit version 1.26.0. My code: Login.py

import streamlit as st
import streamlit_authenticator as stauth
import yaml

# Load Authenticator Configuration
with open('config.yaml') as file:
     config = yaml.load(file, Loader=stauth.SafeLoader)

def login():

 # Authenticator Parameters
 authenticator = stauth.Authenticate(
    config['credentials'],
    config['cookie']['name'],
    config['cookie']['key'],
    config['cookie']['expiry_days']
 )

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

 if st.session_state["authentication_status"]:
    authenticator.logout('Logout', 'main')
    st.write(f'Welcome *{st.session_state["name"]}*')
    st.title('Some content')
    return true
 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 false

main.py

def main():
    st.title("Main page")
    print("hello")
if __name__ == "__main__":
    if login():
        main()

When i login, text "hello" print two times. It look likes there are two threads calling in same time. Am i using wrong ? Please help, thank you.

MadigRG commented 4 months ago

Hi @mvn-hoangnguyen-hn, Have you solved your problem ?

Hi @mkhorasani, I have similar problem. Login needs 2 clicks and so generates 2 API calls. New users calling the API for the first time are supposed to have a specific display. This behavior is impossible due to the double execution.

Any help is welcome.

ShuaHousetable commented 3 months ago

Any updates on this issue?

AngelicSage commented 1 month ago

Every time the session state changes, streamlit reruns the page, so that's probably what's going on, making the code run over and over again, I'm going to try to fix it