mkhorasani / Streamlit-Authenticator

A secure authentication module to validate user credentials in a Streamlit application.
Apache License 2.0
1.37k stars 229 forks source link

Getting random_cookie_name error #134

Closed hunaidkhan2000 closed 3 months ago

hunaidkhan2000 commented 4 months ago

Hi @mkhorasani thanks for this amazing package, I kindly request your assistance with an issue that's surfaced with a Streamlit app we've recently deployed on Azure cloud. From yesterday, we've been encountering a peculiar 'Random_cookie_name' error that's preventing some users from logging out. It's interesting to note that it's not a universal issue - some of our users from different geographical locations are able to logout without trouble. To assist you in troubleshooting, I'm attaching a screenshot along with a sample yml file and script. Your expertise and guidance on this matter would be greatly appreciated. image yml file

credentials:
  usernames:
    hk:
      email: hk@gmail.com
      name: hkhan
      password: "my hashed password" # 
##some other user names 
#file ends with 
cookie:
  expiry_days: 0
  key: "random_signature_key" # Must be a string
  name: "random_cookie_name" #we did not change it , kept it as random_cookie_name 
preauthorized:
  emails:
  - hk@gmail.com

our main file authentication code is

with open('chatbot_users.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']
)
mkhorasani commented 4 months ago

Hi @hunaidkhan2000, thank you for reaching out. Can you check to see if the associated reauthentication cookie is available on the browser when this exception is thrown? You can do this by hitting F12 and typing document.cookie in the console of your browser.

hunaidkhan2000 commented 4 months ago

Hi @mkhorasani , I am not able to find document.cookie in my browser console. one more thing i am using streamlit-authenticator version 0.2.3

mkhorasani commented 4 months ago

Is this how you're trying to access your cookies? image

hunaidkhan2000 commented 4 months ago

@mkhorasani we tried this method and one more also , Attaching the screenshot . Any idea?

image

mkhorasani commented 4 months ago

Okay, by the looks of it, the reauthentication cookie has already been deleted. What I can do is that in a future release, when the library attempts to delete a cookie that has already been deleted, instead of throwing a fatal error I can throw a non-fatal error that allows the user to continue using the application while the error is logged on the console for the developer's notice. I think this would be a reasonable fix for the time being.

hunaidkhan2000 commented 4 months ago

What steps should I take in the meantime? It appears that the issue is intermittent as some users can log out without problems, while others encounter a cookie error that prevents them from logging out every time. There is a possibility that users who are able to log out might have to deal with this issue @mkhorasani

mkhorasani commented 4 months ago

You can try to place the logout() function within a try and except block. The new release should be made very soon.

angelocamacho commented 4 months ago

I have this issue consistently when hosting on Streamlit cloud, but it works fine when hosting locally. I'm not seeing the cookie getting written when hosting on Streamlit cloud for me. Same issue across browsers as well. Tried Chrome and Firefox.

pspiagicw commented 4 months ago

I am facing a similar issue. Is there a temporary fix for this ?

mkhorasani commented 4 months ago

I have this issue consistently when hosting on Streamlit cloud, but it works fine when hosting locally. I'm not seeing the cookie getting written when hosting on Streamlit cloud for me. Same issue across browsers as well. Tried Chrome and Firefox.

Which version of Streamlit-Authenticator and Extra-Streamlit-Components are you using?

angelocamacho commented 4 months ago

Collecting streamlit-authenticator

Downloading streamlit_authenticator-0.3.1-py3-none-any.whl (17 kB)

mkhorasani commented 4 months ago

I'm going to release v0.3.2 in the coming days, with the new version of Extra-Streamlit-Components v0.1.70 (the library that manages the cookies), hopefully these problems should be resolved.

hunaidkhan2000 commented 4 months ago

@mkhorasani do let us know when you release the next version, that will help us

elmighetto commented 4 months ago

Having the same issue. Solution I'm using:

class FixedAuthenticate(stauth.Authenticate): 
  def _implement_logout(self):
        # Clears cookie and session state variables associated with the logged in user.
        try:
            self.cookie_manager.delete(self.cookie_name)
        except Exception as e: 
            print(e)
        self.credentials['usernames'][st.session_state['username']]['logged_in'] = False
        st.session_state['logout'] = True
        st.session_state['name'] = None
        st.session_state['username'] = None
        st.session_state['authentication_status'] = None

authenticator = FixedAuthenticate(...) 

It's a temporary fix that doesn't address the underlying issue of the cookie getting lost in the first place, but it does let the logout complete even if the issue occurs.

Explanation: inherit the Authenticate class, change the loggout implementation to catch exception from deleting the cookie.

hunaidkhan2000 commented 4 months ago

Having the same issue. Solution I'm using:

class FixedAuthenticate(stauth.Authenticate): 
  def _implement_logout(self):
        # Clears cookie and session state variables associated with the logged in user.
        try:
            self.cookie_manager.delete(self.cookie_name)
        except Exception as e: 
            print(e)
        self.credentials['usernames'][st.session_state['username']]['logged_in'] = False
        st.session_state['logout'] = True
        st.session_state['name'] = None
        st.session_state['username'] = None
        st.session_state['authentication_status'] = None

authenticator = FixedAuthenticate(...) 

It's a temporary fix that doesn't address the underlying issue of the cookie getting lost in the first place, but it does let the logout complete even if the issue occurs.

Explanation: inherit the Authenticate class, change the loggout implementation to catch exception from deleting the cookie.

Thank you so much, this worked and resolved my issue

yhavin commented 3 months ago

Having the same issue. Solution I'm using:

class FixedAuthenticate(stauth.Authenticate): 
  def _implement_logout(self):
        # Clears cookie and session state variables associated with the logged in user.
        try:
            self.cookie_manager.delete(self.cookie_name)
        except Exception as e: 
            print(e)
        self.credentials['usernames'][st.session_state['username']]['logged_in'] = False
        st.session_state['logout'] = True
        st.session_state['name'] = None
        st.session_state['username'] = None
        st.session_state['authentication_status'] = None

authenticator = FixedAuthenticate(...) 

It's a temporary fix that doesn't address the underlying issue of the cookie getting lost in the first place, but it does let the logout complete even if the issue occurs.

Explanation: inherit the Authenticate class, change the loggout implementation to catch exception from deleting the cookie.

This works well for avoiding the fatal error, but it looks like authentication status isn't maintained because the cookie isn't found. So I have to log in every time I open the app. Is that a logical consequence of this cookie bug or am I making a different mistake?

elmighetto commented 3 months ago

This works well for avoiding the fatal error, but it looks like authentication status isn't maintained because the cookie isn't found. So I have to log in every time I open the app. Is that a logical consequence of this cookie bug or am I making a different mistake?

Yeah if the cookie is failing to get stored, then it wouldn't be able to keep you logged in. I wonder if this issue has to do with what browser you're using? I haven't tested with anything other than google chrome personally.

yhavin commented 3 months ago

This works well for avoiding the fatal error, but it looks like authentication status isn't maintained because the cookie isn't found. So I have to log in every time I open the app. Is that a logical consequence of this cookie bug or am I making a different mistake?

Yeah if the cookie is failing to get stored, then it wouldn't be able to keep you logged in. I wonder if this issue has to do with what browser you're using? I haven't tested with anything other than google chrome personally.

When I set expiry days to a number, then the cookie stored. I had it at 0 before, because I wanted indefinite maintain of login, but it didn't store.

xixiaoguai727 commented 3 months ago

😭Hi @mkhorasani , may I ask when will the new version will be released?

mkhorasani commented 3 months ago

😭Hi @mkhorasani , may I ask when will the new version will be released?

Apologies for the delay, still working on it.

xixiaoguai727 commented 3 months ago

😭Hi @mkhorasani , may I ask when will the new version will be released?

Apologies for the delay, still working on it.

No need to apologies, you did a great work for us❤️! Just take your time!

jalkestrup commented 3 months ago

Thanks for the post, I am facing a similar bug when using the logout, cookie key error. I can reproduce the error if I log in, then log out, and then log in, and try to log out again. I will go with the try fix for now, thanks.

afederici75 commented 3 months ago

Having the same issue. Solution I'm using:

class FixedAuthenticate(stauth.Authenticate): 

Thank you so much!

titoausten commented 3 months ago

Having the same issue. Solution I'm using:

class FixedAuthenticate(stauth.Authenticate): 
  def _implement_logout(self):
        # Clears cookie and session state variables associated with the logged in user.
        try:
            self.cookie_manager.delete(self.cookie_name)
        except Exception as e: 
            print(e)
        self.credentials['usernames'][st.session_state['username']]['logged_in'] = False
        st.session_state['logout'] = True
        st.session_state['name'] = None
        st.session_state['username'] = None
        st.session_state['authentication_status'] = None

authenticator = FixedAuthenticate(...) 

It's a temporary fix that doesn't address the underlying issue of the cookie getting lost in the first place, but it does let the logout complete even if the issue occurs.

Explanation: inherit the Authenticate class, change the loggout implementation to catch exception from deleting the cookie.

Beautiful!, this solved the bug. Thanks

mkhorasani commented 3 months ago

Dear all, this issue has now been fixed in the latest release v0.3.2. Thank you for your patience