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

newly created pwd from function reset_password doesn't update new pwd in config.ymal #29

Closed jitvimol closed 1 year ago

jitvimol commented 1 year ago

When user edit password, it seems to store new pwd in memory. If user log out, he cannot log in with newly created pwd.

mkhorasani commented 1 year ago

Can you please provide a snippet of your code?

jitvimol commented 1 year ago

Can you please provide a snippet of your code?

Sure Thanks for the help:

I simply add function to reset pwd right after user successfully log in and test logging out.

import streamlit as st
import streamlit_authenticator as stauth
import yaml
from yaml import SafeLoader

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

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

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

if st.session_state["authentication_status"]:
    authenticator.logout('Logout', 'main')
    st.title(f'Welcome *{st.session_state["name"]}*')

    if authentication_status:
        try:
            if authenticator.update_user_details(username, 'Update user details'):
                st.success('Entries updated successfully')
        except Exception as e:
            st.error(e)

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')
mkhorasani commented 1 year ago

Pleas refer to point 9 of the readme file, once you have updated the config file on memory, you must then commit it to disk.

jitvimol commented 1 year ago

Pleas refer to point 9 of the readme file, once you have updated the config file on memory, you must then commit it to disk.

Thank you very much. Apologize for not fully understand the library.

colelandolt commented 1 year ago
import streamlit as st
import streamlit_authenticator as stauth
import yaml
from yaml import SafeLoader

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

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

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

if st.session_state["authentication_status"]:
    authenticator.logout('Logout', 'main')
    st.title(f'Welcome *{st.session_state["name"]}*')

    if authentication_status:
        try:
            if authenticator.update_user_details(username, 'Update user details'):
                st.success('Entries updated successfully')
        except Exception as e:
            st.error(e)

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')

@jitvimol Where did you insert the code from the readme that updates config.yaml within the above code snippet? I'm having trouble implementing this functionality in my streamlit app.

mkhorasani commented 1 year ago

Here is the snippet that saves your changes:

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