How to Get Updated Cookies on st.rerun Without Full Page Refresh?

Hi everyone,

I’m developing an application with authentication and encountering an issue with handling cookies. My use case is following:

  • My “demo” application is hosted on the server where the “main” website is hosted, i.e:
    • - main application that has authentication form.
    • - streamlit application.
  • User logs-in in the main application obtaining auth token cookie (i.e. auth_token)
  • Demo application validates cookies on each update, and if auth token cookie is absent, expired or invalid - it redirects to “main” website login form or stops.

The problem: Cookies do not get updated in streamlit after they are changed in browser or by logging out in the “main” website.

  • They are updated though when the page reload is done i.e. F5.

I need them to be updated after st.rerun or any other streamlit page update. The idea is to stop the application on the whatever next action after the token is invalid.

Here is an example code I created to show the issue:

import streamlit as st
import extra_streamlit_components as stx

cookie_manager = stx.CookieManager(key=0)

# Simulate authentication
cookies = cookie_manager.get_all(key=1)
if "auth_token" not in cookies:
    cookie_manager.set("auth_token", "123", key=2)

# Get cookies
all_cookies = cookie_manager.get_all(key=3)

# Change cookie in browser to whatever else value 
# (assume they are changed or deleted after log out in "main" website)

# Perform action equivalent to st.rerun:
# - press R to rerun the app or make a widget update...

# Cookies are changed in browser but not in streamlit after widget update or st.rerun
# This means that logged out user can still continue using the application until 
# full page reload which is not acceptable.

I am using extra_streamlit_components.CookieManager as it seems to be the most supported tool for managing cookies in Streamlit. If there are better tools or approaches for achieving the desired outcome, I am open to suggestions.


# Python 3.9

I am new to authentication and cookie handling, so please excuse any mistakes in my approach or explanation, and feel free to suggest improvements!

Thank you for your help!