Streamlit-Authenticator is not working

I’m building a basic streamlit app. In order to validate user credentials, I’m using the authentification module of streamlit. Only one problem, the code does not work !

ERROR

TypeError: Authenticate.init() got multiple values for argument ‘cookie_expiry_days’

Traceback:

File “C:\Users_M92\Desktop\Coding\Python\Projects\Personal1\venv\lib\site-packages\streamlit\scriptrunner\script_runner.py”, line 557, in _run_script exec(code, module.dict)

File “app.py”, line 23, in
authenticator = stauth.Authenticate(names, usernames, hashed_passwords,

By the way, I used the same code described here in the official documentation :

CODE

    import streamlit as st
    import streamlit_authenticator as stauth
    
    names = ['John Smith', 'Rebecca Briggs']
    usernames = ['jsmith', 'rbriggs']
    passwords = ['123', '456']
    
    hashed_passwords = stauth.Hasher(passwords).generate()
    
    authenticator = stauth.Authenticate(names, usernames, hashed_passwords,
        'some_cookie_name', 'some_signature_key', cookie_expiry_days=30)
    
    name, authentication_status, username = authenticator.login('Login', 'main')
    
    if authentication_status:
        authenticator.logout('Logout', 'main')
        st.write('Welcome *%s*' % (name))
        st.title('Some content')
    elif authentication_status == False:
        st.error('Username/password is incorrect')
    elif authentication_status == None:
        st.warning('Please enter your username and password')
    
    if st.session_state['authentication_status']:
        st.write('Welcome *%s*' % (st.session_state['name']))
        st.title('Some content')
    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')

When I removed the argument cookie_expiry_days=30, I get this error instead :

TypeError: list indices must be integers or slices, not str

File"C:\Users_M92\Desktop\Coding\Python\Projects\Personal1\venv\lib\site-packages\streamlit_authenticator\authenticate.py",line
36, in __init__self.credentials[‘usernames’] = {key.lower(): value for
key, value in credentials[‘usernames’].items()}

Do you have any idea why I keep getting these errors ?

1 Like

Do check the documentation. The component had changed - it now uses a dict as part of the Authenticator.

1 Like

hi all ,

see my solution: the sames values from config.yaml file

list_usernames = [‘jsmith’, ‘rbriggs’]
list_email = [‘jsmith@gmail.com’, ‘rbriggs@gmail.com’]
list_name = [‘John Smith’, ‘Rebecca Briggs’]
list_passwords = [“123”, “456”] # 123 456 to be replaced by hashed values
list_emails_prehautorized = [‘melsby@gmail.com’]
list_value_cookies = [30, ‘random_signature_key’, ‘random_cookie_name’]

def autentificator_list_dict(list_usernames_, list_email_, list_name_, list_passwords_, list_emails_prehautorized_, list_value_cookies_):
list_user = [‘email’, ‘name’, ‘password’]
list_cookies = [‘expiry_days’, ‘key’, ‘name’]
list_value_prehautorized = {‘emails’: list_emails_prehautorized_}

# generation user list
l_user_values = []
for n in range ( len ( list_user ) - 1 ):
    l_user_values.append ( [list_email_[n], list_name_[n], list_passwords_[n]] )

# list to dict
credentials = {}
usernames = {}
cookie = {'cookie': dict ( zip ( list_cookies, list_value_cookies_ ) )}
prehautorized = {'preauthorized': list_value_prehautorized}

user_values = {}
for n in range ( len ( list_usernames_ ) ):
    usernames[list_usernames_[n]] = dict ( zip ( list_user, l_user_values[n] ) )

usernames = {'usernames': usernames}
config = {'credentials': usernames, **cookie, **prehautorized}  # merge dict
return config

and use the def autentificator_list_dict():

config = autentificator_list_dict(list_usernames,list_email,list_name,list_passwords,list_emails_prehautorized,list_value_cookies)

and now :

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

:slight_smile:

hey man im getting this error

Traceback (most recent call last):
  File "C:\Users\elais\OneDrive\Desktop\Fantasy\main.py", line 23, in <module>
    name, authentication_status, username = authenticator.login("Login", "main")
 raise KeyError(_missing_key_error_message(key))
KeyError: 'st.session_state has no key "authentication_status". Did you forget to initialize it?

i used the method you wrote and i dont know how to fix it. help please if you can

Please share the code where you instantiate your streamlit_authenticator object.