mkhorasani / Streamlit-Authenticator

A secure authentication module to manage user access in a Streamlit application.
Other
1.66k stars 258 forks source link

Get password when registering user #213

Closed cdagher closed 1 month ago

cdagher commented 1 month ago

Hello, I am having some trouble with the register user function. When calling authenticator.register_user I am unable to write the user's password to config.yaml. I have some custom fields added to the config for role and organization, but have also tried this without these fields and am running into the same issue. I temporarily resorted to setting password to an empty string and handling this elsewhere in the code, but this isn't a great solution. A snippet of my code is attached below:

@st.dialog("Add User")
def add_user():
    new_role = st.selectbox("Role", ["User", "Admin"])
    new_email, new_username, new_name = st.session_state.authenticator.register_user(captcha=False, pre_authorization=False)

    if new_email is None or new_username is None or new_name is None:
        return
    new_org = st.session_state.org

    config['credentials']['usernames'][new_username] = {
        'name': new_name,
        'email': new_email,
        'password': '',
        'role': new_role,
        'org': new_org
    }
    with open('config.yaml', 'w') as file:
        yaml.dump(config, file, default_flow_style=False)
    st.rerun()

Any help would be appreciated. Thanks!

mkhorasani commented 1 month ago

Hi @cdagher, please try removing the st.rerun().

cdagher commented 1 month ago

Thanks for the suggestion @mkhorasani. Unfortunately, this didn't solve the problem. Is there anything else that I can try?

mkhorasani commented 1 month ago

Thanks for the suggestion @mkhorasani. Unfortunately, this didn't solve the problem. Is there anything else that I can try?

Update to the latest version, and supply the config file as a path to the Authenticate class:

authenticator = stauth.Authenticate(
    '../config.yaml'
)

This way you also won't need to read/write to the config file, Streamlit-Authenticator will take care of it automatically for all widgets.

cdagher commented 1 month ago

That works. Thank you @mkhorasani!