Extra-Streamlit-Components
Extra-Streamlit-Components copied to clipboard
Store Cookie Manager in Session State
st.session_state
can be used to store persistence variables that will stay the same during reruns.
The cool thing is that every user/session (A session is a browser Tap in this case) has its own Session State.
You currently can't use the Cookie Manager really in Shared Environments
Security Note: In shared domains such as share.streamlit.io, other web developers can have access to the cookies you set and the same goes for you. This is not to be treaded as security bug but a circumstance the developer need to be aware of.
If I understand correctly, this is because the cookie manager stores a local copy of all cookies.
https://github.com/Mohamed-512/Extra-Streamlit-Components/blob/9bd08dc4059952dab1a95a6e0727ccd21eb6b60a/extra_streamlit_components/CookieManager/init.py#L19
class CookieManager:
def __init__(self, key="init"):
self.cookie_manager = _component_func
self.cookies = self.cookie_manager(method="getAll", key=key, default={})
But by using Session State each user would get their own Manager → No Leaked Cookies.
if "cookie_manager" not in st.session_state:
st.session_state.cookie_manager = stx.CookieManager(
key="cookie_manager",
)
Am I missing something, or would this be a better approach? @Mohamed-512
You can't use cookies in shared environments, because cookies are tied to the domain in user's browser (i.e. on client side). Session state has nothing to do with that at all. Shared environment needs a way to separate cookies. See my answer to you in another issue, where you asked for a link.