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

reset password does not work #98

Closed LiminLN closed 7 months ago

LiminLN commented 7 months ago

I tried to use reset password as below but it does not successed without any infomaiton or error :

def modify_password(authenticator,username):

with open(path + 'config.yaml') as file:
    config = yaml.load(file, Loader=SafeLoader)

if st.session_state['user_authenticated']:       
    try:
        if authenticator.reset_password(username, 'Reset password'):
            st.success('Password modified successfully') 
    except Exception as e:
        st.error(e)

with open(path + 'config.yaml', 'w') as file:
    yaml.dump(config, file, default_flow_style=False)
mkhorasani commented 7 months ago

Hi can you please share your entire script if possible?

LiminLN commented 7 months ago

def login(authenticator): name, authentication_status, username = authenticator.login('Login', 'main') if authentication_status: authenticator.logout('Logout', 'sidebar', key='unique_key') if 'user_authenticated' not in st.session_state: st.session_state['user_authenticated'] = True if 'username' not in st.session_state: st.session_state['username'] = username tabs_upload_search_and_give_like(username) if st.sidebar.button('Modify password'): modify_password(authenticator,username) elif authentication_status == False: st.error('Username/password is incorrect')

def modify_password(authenticator,username):

with open(path + 'config.yaml') as file:
    config = yaml.load(file, Loader=SafeLoader)

if st.session_state['user_authenticated']:
    try:
        if authenticator.reset_password(username, 'Reset password'):
            st.success('Password modified successfully')
    except Exception as e:
        st.error(e)

with open(path + 'config.yaml', 'w') as file:
    yaml.dump(config, file, default_flow_style=False)

def main():

st.set_page_config(
#    page_title="Ex-stream-ly Cool App",
#    page_icon="🧊",
    layout="wide",
#    initial_sidebar_state="expanded",
    menu_items={
    # 'Get Help': 'https://www.extremelycoolapp.com/help',
    # 'Report a bug': "https://www.extremelycoolapp.com/bug",
    'About': "# This is an *extremely* cool app!"
    }
)

hide_st_style = """
            <style>
            #MainMenu {visibility: hidden;}
            footer {visibility: hidden;}
            <style>
            """
st.markdown(hide_st_style, unsafe_allow_html=True)

with open(path + 'config.yaml') as file:
    config = yaml.load(file, Loader=SafeLoader)

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

login(authenticator)

if name == "main": main()

LiminLN commented 7 months ago

Hi can you please share your entire script if possible?

Yes,I added it above.

LiminLN commented 7 months ago

Hi can you please share your entire script if possible? What's the check_credentials() in reset_password() in authenticate.py for ? I think the reason is check_credentials() returns false.

    if reset_password_form.form_submit_button('Reset'):
        if self._check_credentials(inplace=False):
            if len(new_password) > 0:
            ........
MvE10 commented 7 months ago

Hi @LiminLN, make sure to check if you're writing to the right config file. You can easily debug your code by trying to change the file locally with yaml.dump. Also, I've put it within the write to .yaml piece within the if success part of the code:


                    with open('config.yaml', 'w') as file:
                        print('open yaml config')
                        yaml.dump(config, file, default_flow_style=False)
                        print('yaml config saved')
                    st.success('User registered successfully')```
LiminLN commented 7 months ago

Hi @LiminLN, make sure to check if you're writing to the right config file. You can easily debug your code by trying to change the file locally with yaml.dump. Also, I've put it within the write to .yaml piece within the if success part of the code:

                    with open('config.yaml', 'w') as file:
                        print('open yaml config')
                        yaml.dump(config, file, default_flow_style=False)
                        print('yaml config saved')
                    st.success('User registered successfully')```

Thank you ! I tried the code above and the config.yaml file doesnot opened.

LiminLN commented 7 months ago

Hi @LiminLN, make sure to check if you're writing to the right config file. You can easily debug your code by trying to change the file locally with yaml.dump. Also, I've put it within the write to .yaml piece within the if success part of the code:

                    with open('config.yaml', 'w') as file:
                        print('open yaml config')
                        yaml.dump(config, file, default_flow_style=False)
                        print('yaml config saved')
                    st.success('User registered successfully')```

Thank you ! I tried the code above and the config.yaml file doesnot opened.

IT is solved today in this way:

create a new file named "4_modify_password.py":

path = 'D:\PycharmProjects\pythonProject\streamlit\triathlon\'

with open(path + 'config.yaml') as file: config = yaml.load(file, Loader=SafeLoader)

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

try: if authenticator.reset_password(st.session_state['username_login'], 'Reset password'): st.success('Password modified successfully') except Exception as e: st.error(e)

with open(path + 'config.yaml', 'w') as file: yaml.dump(config, file, default_flow_style=False)