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

Create more than 1 cookie depending on the user #129

Closed SGaryx closed 3 months ago

SGaryx commented 5 months ago

Hello, I would like to create 2 different cookies depending on the user logged in so that one "super user" is allowed to stay 1 week before re-authentificate and the others just have 1 day. I did not find anything about this on the web, could you help me ?

Thanks a lot

mkhorasani commented 3 months ago

Hi @SGaryx, this is a feature that is not currently supported and I am not sure whether I will be able to implement it anytime soon either. Instead, I recommend that you look into implementing a work around using the extra_streamlit_component's cookie manager. All you would need to do is to overwrite the current cookie and modify the expiry date. You can use the following snippet to write your own cookie:

cookie_manager = stx.CookieManager()
token = jwt.encode({'username': <username>, 'exp_date': (datetime.utcnow() + timedelta(days=<cookie_expiry_days>)).timestamp()}, cookie_key, algorithm='HS256')
cookie_manager.set(cookie_name, token, expires_at=datetime.now() + timedelta(days=cookie_expiry_days))