Hi, I’ve started using cookies for user identification / authentication (on a very basic level)
A compressed version of the code looks like this:
from uuid import uuid4
from streamlit_cookies_controller import CookieController
controller = CookieController()
# some code in which the user signs in with a username and password
# ...
# ...
uuid = f"{uuid4()}"
controller.set("uuid", uuid) # after authentication: store a unique ID in the cookie
# now here is some code that stores the uuid on the server side (in a file)
# ...
# ...
Now when the user comes back, the following happens:
cookies = controller.getAll()
uuid_from_cookies = cookies.get("uuid", None)
# here is some code that will check the uuid from the cookie
# against the list of uuids that the server has stored (in a file).
# if the uuid from the cookie matches with one of the uuids t
# hat the server holds in a file, the user is automatically signed in
In most cases this works, but was baffles me is that restarting streamlit seems to clear the client-side cookies? I have some debug printouts that list the uuid from the cookie and the list of uuids that the server knows. After restarting streamlit, cookies = controller.getAll() results in an empty dictionary. Should cookies not survive a server restart?
Furhtermore, restarting the browser (Chrome) sometimes signs me out as well, but sometimes it keeps me signed it (again sometimes controller.getAll() results in {}.
I’m not familiar with that custom component specifically, but no files are preserved with a reboot on Community Cloud. Your environment will be reset to a fresh download from your Git repository. The custom component would have to use some external storage in order for IDs to persist through a reboot.
Yes, max age is set to 7 * 86400 (which should be one full week)
Does the CookieController really issue cookies to the clients (= saving cookies in the client browser)? If so, is the browser (Chrome) perhaps messing with the cookies?
expires was where the one-day expiration default came from. Can you pass today plus one week and see if that helps?
I’m not familiar with the library, but that’s what I expect from cookies. And yes, that means a user can clear away their cookies (manually or through an automated setting).
Thanks for the suggestions! It seems the problem was completely unrelated:
I had offloaded the cookie controller and the handling of cookies to a separate python file. Importing this file into my main page and sub-pages caused some pretty unpredictable behavior:
user got signed out seemingly at random
user got signed out upon refreshing the page
user got signed out upon closing and reopening the browser
And worst of all:
user2 could sign in as user1 without the correct credentials
It appears instantiating the cookie controller in an auxiliary python file makes its behavior pretty unpredictable. It also leads to multiple users being able to “share” their cookies (I suppose on the server side the cookie controller instantiated just once when my auxiliary python file was imported for the first time, and subsequent sessions would all use the same instance of the cookie controller)
After modifying my script and handling the cookies in the main page python file, the expected behavior was achieved.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.