Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fsmosca opened this issue Jan 24, 2024 · 5 comments
Closed

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

fsmosca opened this issue Jan 24, 2024 · 5 comments

Comments

@fsmosca
Copy link

fsmosca commented Jan 24, 2024

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
Copy link
Owner

mkhorasani commented Jan 24, 2024

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
Copy link

SGaryx commented Jan 24, 2024

Where do we need to write this plz ?

@mkhorasani
Copy link
Owner

mkhorasani commented Jan 24, 2024

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

@mkhorasani
Copy link
Owner

mkhorasani commented Jan 25, 2024

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
Copy link
Author

fsmosca commented Jan 25, 2024

Thanks for the update, I will try that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants