streamlit-keycloak icon indicating copy to clipboard operation
streamlit-keycloak copied to clipboard

Multipage app

Open prki opened this issue 1 year ago • 4 comments

Hey, not sure if this is an issue or rather a support topic, but I have not been able to find anything anywhere so I figured out this might be the best place to ask.

TL;DR - How do I use streamlit-keycloak with a multi-page app?

Detailed Q: When doing the example flow, such as:

keycloak = login(...)

if keycloak.authenticated:
    render_page()
else:
    display_not_auth()

This does work, but with two significant caveats:

  1. login() seems to be async under the hood. Even though the Python function is synchronous, it appears the behavior is that once the data is fetched, the page is re-rendered - with the data being populated, somehow.
  • That is fine for first-page entry because authenticated == false until keycloak resolves this. However, I noticed strange behavior where selecting a particular page results in double re-rendering - it appears that keycloak is already considered authenticated, retrieving some base value (what has already been set?), keycloak.authenticated is resolved as true and then new token is retrieved. On refresh, this behaves on default. I do not understand Streamlit's inner working well to understand where this data is stored.
  1. login() is called each time on every single page. Every single page gets a new token, which it does not need to get - and the token could just be reused.

As such, is there any recommended approach how to use this component for multi-page apps? I have a few ideas, but they feel like bending the component a bit - for instance having a separate class managing the token and populating the value and the class being in st.session_state - unsure if that would even work. Or if there is a callback that can be passed as a parameter which would be used to populate the resulting keycloak dataclass into the session state and then use that.

Is there a recommended approach?

prki avatar Aug 31 '23 13:08 prki