Multiple Sessions Issue with Supabase Auth

Multiple Sessions Issue with Supabase Auth

Hey Streamlit team, thanks for all of the great work!

I’m attempting to add authentication using supabase according to the docs here, but I seem to be missing something.

When a user logs in on their session, that session is persisted across all other users. Two different users can’t be logged in at the same time. And any visitor is automatically authenticated if someone else is logged in.

I believe the problem comes from this line of code here:

if supabase.auth.get_user():

This returns the latest user across all sessions, not necessarily the active user’s session.

Loom Demo Video:
Problem Explanation Video

Example App URL:
https://supabase-test.streamlit.app

GitHub Repo:
https://github.com/CBell045/Test

Streamlit Version: streamlit==1.28.2
Python 11

Thanks for your help on this!

Hi Chad_Bell, I experienced the same problem as you. What I did in the end was the following (there might be a better solution):

I abstracted the supabase client as follows:

url: str = os.environ.get("SUPABASE_URL")
key: str = os.environ.get("SUPABASE_KEY")

class SupabaseEngine:
    def __init__(self, url, key):
        self.supabase: Client = create_client(url, key)

And then stored the instance in the session_state:

if "supabase" not in st.session_state:
     st.session_state['supabase'] = SupabaseEngine(url, key).supabase

The downside is that when a user hits the refresh button, then the user is logged out.

Hope this helps.

BR,
Sam

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.