Streamlit-Authenticator icon indicating copy to clipboard operation
Streamlit-Authenticator copied to clipboard

Constant authentication_status == False

Open aishworyann opened this issue 2 years ago • 16 comments
trafficstars

hi , i was just working with streamlit-authenticator and was making a login page usng config.yaml , but it always outputing the authentication_status as False , however i have done exactly the same as there was in docs. Please help me to resolve this. Also while deploying it on streamlit cloud, it was not getting the credentials from the repo .

aishworyann avatar Apr 24 '23 01:04 aishworyann

Hi @aishworyann, if possible can you please share your source code and perhaps also your config file?

mkhorasani avatar Apr 24 '23 09:04 mkhorasani

Yeah sure @mkhorasani , and thanks for replying sir.Below i am attaching screenshots of the code. Screenshot 2023-04-25 at 12 05 12 AM Screenshot 2023-04-25 at 12 05 20 AM Screenshot 2023-04-25 at 12 05 01 AM Sir , i have tried using your given default example code too but same thing is happening.

aishworyann avatar Apr 24 '23 18:04 aishworyann

Below i have updated the config.yaml file using the hash generated and which i have used after updating the hash value(ignore the upper config file) Screenshot 2023-04-25 at 12 08 00 AM

aishworyann avatar Apr 24 '23 18:04 aishworyann

If possible please share the source code as a text file, it'll be a lot easier for me to debug it that way. Thanks.

mkhorasani avatar Apr 26 '23 08:04 mkhorasani

I have the same issue.

mnsosa avatar May 07 '23 21:05 mnsosa

Can you please share your source code if possible.

mkhorasani avatar May 08 '23 05:05 mkhorasani

Same issue here too, my source code is simple as your readme :

app.py

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
from config import debug_mode
from airtable_utils import fetch_data_from_airtable

import folium
import streamlit_folium as stf

from pages import docs

# Set the title of the app
st.title("Demo Streamlit Auth")

with open('./config.yml') 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']
)

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

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')
    docs.show()

config.yml

cookie:
  expiry_days: 30
  key: joiquclqsijflqsknjvqeluihfjomIJNQOIEJCRFGLHGFKEQOIMZXfJFO
  name: streamlit-auth-cookie
credentials:
  usernames:
    charly:
      email: [email protected]
      name: Buddy Sample
      password: $2b$12$5IZZ5MM9jNi/CCaQE9Eq4ufKMbKmCxqZbvm2aQOvu1nau871TuqSO
    romain:
      email: [email protected]
      name: John Doe
      password: $2b$12$znk0zJpJmkhnajKZHjyCiujVGrDDiCeQypwXva/zsGY2H9M8dI/DO
preauthorized:
  emails:
  - [email protected]

C4rlit0 avatar May 09 '23 08:05 C4rlit0

Same issue here too, my source code is simple as your readme :

app.py

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
from config import debug_mode
from airtable_utils import fetch_data_from_airtable

import folium
import streamlit_folium as stf

from pages import docs

# Set the title of the app
st.title("Demo Streamlit Auth")

with open('./config.yml') 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']
)

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

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')
    docs.show()

config.yml

cookie:
  expiry_days: 30
  key: joiquclqsijflqsknjvqeluihfjomIJNQOIEJCRFGLHGFKEQOIMZXfJFO
  name: streamlit-auth-cookie
credentials:
  usernames:
    charly:
      email: [email protected]
      name: Buddy Sample
      password: $2b$12$5IZZ5MM9jNi/CCaQE9Eq4ufKMbKmCxqZbvm2aQOvu1nau871TuqSO
    romain:
      email: [email protected]
      name: John Doe
      password: $2b$12$znk0zJpJmkhnajKZHjyCiujVGrDDiCeQypwXva/zsGY2H9M8dI/DO
preauthorized:
  emails:
  - [email protected]

The below code works fine on my end with streamlit-authenticator==0.2.2, can you try this?

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
import yaml
#from config import debug_mode


# Set the title of the app
st.title("Demo Streamlit Auth")

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

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

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')

mkhorasani avatar May 09 '23 10:05 mkhorasani

Hi @mkhorasani !

I've copy/paste your code, but when I entrer login/pwd I've received this error :

2023-05-09 13:39:34.287 Uncaught app exception
Traceback (most recent call last):
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
  File "/home/charly/Dev/KroKee/streamlit-auth/app.py", line 22, in <module>
    name, authentication_status, username = authenticator.login('Login', 'sidebar')
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit_authenticator/authenticate.py", line 194, in login
    return st.session_state['name'], st.session_state['authentication_status'], st.session_state['username']
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit/runtime/state/session_state_proxy.py", line 90, in __getitem__
    return get_session_state()[key]
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit/runtime/state/safe_session_state.py", line 111, in __getitem__
    raise KeyError(key)
KeyError: 'name'

C4rlit0 avatar May 09 '23 11:05 C4rlit0

Hi @mkhorasani

Same issue here too, my source code is simple as your readme : app.py

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
from config import debug_mode
from airtable_utils import fetch_data_from_airtable

import folium
import streamlit_folium as stf

from pages import docs

# Set the title of the app
st.title("Demo Streamlit Auth")

with open('./config.yml') 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']
)

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

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')
    docs.show()

config.yml

cookie:
  expiry_days: 30
  key: joiquclqsijflqsknjvqeluihfjomIJNQOIEJCRFGLHGFKEQOIMZXfJFO
  name: streamlit-auth-cookie
credentials:
  usernames:
    charly:
      email: [email protected]
      name: Buddy Sample
      password: $2b$12$5IZZ5MM9jNi/CCaQE9Eq4ufKMbKmCxqZbvm2aQOvu1nau871TuqSO
    romain:
      email: [email protected]
      name: John Doe
      password: $2b$12$znk0zJpJmkhnajKZHjyCiujVGrDDiCeQypwXva/zsGY2H9M8dI/DO
preauthorized:
  emails:
  - [email protected]

The below code works fine on my end with streamlit-authenticator==0.2.2, can you try this?

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
import yaml
#from config import debug_mode


# Set the title of the app
st.title("Demo Streamlit Auth")

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

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

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')

Hi @mkhorasani I tried exact same code as yours but still getting None in field name and authentication_status at line <name, authentication_status, username = authenticator.login('Login', 'sidebar')> When I printed these variable I got - name, authentication_status, username - None False buddy sample How to resolve it , please help !!

Siddhijain16 avatar Jun 01 '23 10:06 Siddhijain16

I also get:

none, none,

as the values for name, authentication_status, username when. I try to login. Just following the demo, but in a muilt-page app.

GeorgePearse avatar Aug 17 '23 08:08 GeorgePearse

Hi @mkhorasani !

I've copy/paste your code, but when I entrer login/pwd I've received this error :

2023-05-09 13:39:34.287 Uncaught app exception
Traceback (most recent call last):
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
  File "/home/charly/Dev/KroKee/streamlit-auth/app.py", line 22, in <module>
    name, authentication_status, username = authenticator.login('Login', 'sidebar')
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit_authenticator/authenticate.py", line 194, in login
    return st.session_state['name'], st.session_state['authentication_status'], st.session_state['username']
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit/runtime/state/session_state_proxy.py", line 90, in __getitem__
    return get_session_state()[key]
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit/runtime/state/safe_session_state.py", line 111, in __getitem__
    raise KeyError(key)
KeyError: 'name'

Hi @C4rlit0, please ensure that you are providing the name field in the config file for each user.

mkhorasani avatar Aug 17 '23 08:08 mkhorasani

I also get:

none, none,

as the values for name, authentication_status, username when. I try to login. Just following the demo, but in a muilt-page app.

Can you try to read the authentication status value and other values from the session state instead? i.e. authenticaion_status = st.session_state['authentication_status'].

mkhorasani avatar Aug 17 '23 08:08 mkhorasani

Hi @mkhorasani

Same issue here too, my source code is simple as your readme : app.py

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
from config import debug_mode
from airtable_utils import fetch_data_from_airtable

import folium
import streamlit_folium as stf

from pages import docs

# Set the title of the app
st.title("Demo Streamlit Auth")

with open('./config.yml') 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']
)

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

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')
    docs.show()

config.yml

cookie:
  expiry_days: 30
  key: joiquclqsijflqsknjvqeluihfjomIJNQOIEJCRFGLHGFKEQOIMZXfJFO
  name: streamlit-auth-cookie
credentials:
  usernames:
    charly:
      email: [email protected]
      name: Buddy Sample
      password: $2b$12$5IZZ5MM9jNi/CCaQE9Eq4ufKMbKmCxqZbvm2aQOvu1nau871TuqSO
    romain:
      email: [email protected]
      name: John Doe
      password: $2b$12$znk0zJpJmkhnajKZHjyCiujVGrDDiCeQypwXva/zsGY2H9M8dI/DO
preauthorized:
  emails:
  - [email protected]

The below code works fine on my end with streamlit-authenticator==0.2.2, can you try this?

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
import yaml
#from config import debug_mode


# Set the title of the app
st.title("Demo Streamlit Auth")

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

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

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')

Hi @mkhorasani I tried exact same code as yours but still getting None in field name and authentication_status at line <name, authentication_status, username = authenticator.login('Login', 'sidebar')> When I printed these variable I got - name, authentication_status, username - None False buddy sample How to resolve it , please help !!

Same as above, can you please try to read the authentication status from the session state, i.e. authentication_status = st.session_state['authentication_status'].

mkhorasani avatar Aug 17 '23 08:08 mkhorasani

For me the problem was just that I hadn't yet hashed the password in the config file (but had the original string forms).

I assumed that this would just give me an incorrect password error, but instead just returned none, none, username

GeorgePearse avatar Aug 17 '23 08:08 GeorgePearse

If I use st.session_state['authentication_status'] instead of name, authentication_status, username = authenticator.login('Login', 'main') (I need to show login and register forms separately), I got after the page reloading authentication_status is None. Go deeper in the streamlit-authenticator code I found that it's bc with login() method will be execute _check_cookie() private method that can get data from token and add it to session state.

def login(self, form_name: str, location: str = 'main') -> tuple:
    ...
    if not st.session_state['authentication_status']:
        self._check_cookie()
        ...

_check_cookie() method is mb improved (cleared from all these nested if's), but below I pass it as-is:

    def _check_cookie(self):
        self.token = self.cookie_manager.get(self.cookie_name)
        if self.token is not None:
            self.token = self._token_decode()
            if self.token is not False:
                if not st.session_state['logout']:
                    if self.token['exp_date'] > datetime.utcnow().timestamp():
                        if 'name' and 'username' in self.token:
                            st.session_state['name'] = self.token['name']
                            st.session_state['username'] = self.token[
                                'username']
                            st.session_state['authentication_status'] = True

And now take a look at last line of code: st.session_state['authentication_status'] = True.

Maybe this will help you

art610 avatar Sep 04 '23 14:09 art610

Please see if this problem still persists with the latest release v0.3.2.

mkhorasani avatar Mar 28 '24 11:03 mkhorasani