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

KeyError: False after successful login and refreshing the page #111

Closed fsmosca closed 5 months ago

fsmosca commented 5 months ago

Thanks for the update.

I tried the new 0.2.5 with streamlit 1.30.0 locally and got this error after successful login and pressing the refresh button of the browser.

image

KeyError: False
Traceback:
File "F:\Project\st_authenticator_test\venv\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 535, in _run_script
    exec(code, module.__dict__)
File "F:\Project\st_authenticator_test\main.py", line 19, in <module>
    authenticator.login(location='main', max_concurrent_users=10, fields={'Form name': 'Login Form'})
File "F:\Project\st_authenticator_test\venv\Lib\site-packages\streamlit_authenticator\authenticate.py", line 246, in login
    self._check_cookie()
File "F:\Project\st_authenticator_test\venv\Lib\site-packages\streamlit_authenticator\authenticate.py", line 128, in _check_cookie
    st.session_state['name'] = self.credentials['usernames'][self._get_username('id', self.token['id'])]['name']
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^

How to handle that? I quit streamlit and run it again and I got the same error. I could no longer view the page correctly.

Sample code.

main.py

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

with open('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']
)

authenticator.login(location='main', max_concurrent_users=10, fields={'Form name': 'Login Form'})

if st.session_state["authentication_status"]:
    authenticator.logout()
    st.write(f'Welcome *{st.session_state["name"]}*')
    st.title('Some content')
elif st.session_state["authentication_status"] is False:
    st.error('Username/password is incorrect')
elif st.session_state["authentication_status"] is None:
    st.warning('Please enter your username and password')

config.yaml

cookie:
  expiry_days: 1
  key: qjYbSwPxTbWsHrvS
  name: streamlit_auth_demo
credentials:
  usernames:
    jsmith:
      email: jsmith@gmail.com
      name: John Smith
      password: $2b$12$8rxEVApoHMKkli9mCmJX4u5.m1TiKmfvrSarqxqbis4fRc8bS5pIu
    rbriggs:
      email: rbriggs@gmail.com
      name: Rebecca Briggs
      password: $2b$12$aYBWRShgzRPX0u05eBcSRu5nlZYrVuBJgA/A7tWRFP1nhp.t6igmO
preauthorized:
  emails:
  - melsby@gmail.com

Both config.yaml and main.py are located on same folder.

Sample credential.

username: jsmith
password: abc

requirements.txt

streamlit
streamlit-authenticator==0.2.5
mkhorasani commented 5 months ago

Dear @fsmosca, please make sure to save changes to the config file after every widget as shown below:

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

This is to ensure that the new 'ID' field that is being automatically generated will be saved to the config file.

SGaryx commented 5 months ago

Where do we need to write this plz ?

mkhorasani commented 5 months ago

You can write it at the end of your script, please refer to this file as an example.

mkhorasani commented 5 months ago

Dear @fsmosca and @SGaryx, I have decided to axe Streamlit-Authenticator v0.2.5 and instead replace it with v0.3.1 to resolve this issue. Please download this latest version.

fsmosca commented 5 months ago

Thanks for the update, I will try that.