supabase / supabase-py

Python Client for Supabase. Query Postgres from Flask, Django, FastAPI. Python user authentication, security policies, edge functions, file storage, and realtime data streaming. Good first issue.
https://supabase.com/docs/reference/python
MIT License
1.72k stars 203 forks source link

Causing an auth event raises NotConnectedError because realtime.connect() isn't called beforehand #898

Closed Slumberdac closed 2 months ago

Slumberdac commented 2 months ago

Bug report

Describe the bug

Causing an auth event raises NotConnectedError because realtime.connect() isn't called beforehand. For example, trying to Sign up a user through supabase.auth.sign_up(credentials) makes gotrue notify subscribers of the event. Supabase listens for the event and tries to run set_auth() without connecting and then crashes the application.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Initialize a supabase client
  2. Try signing up a new user
  3. Client tries to run self.realtime.set_auth(access_token) without calling self.realtime.connect() first
  4. NotConnectedError gets raised

Expected behavior

A new user should be created without rising any errors

Screenshots

If applicable, add screenshots to help explain your problem.

System information

Traceback

  File "/mnt/c/Users/Jacob/Documents/GitHub/user-api/app/api/auth.py", line 44, in signup_user
    session = settings.supabase.auth.sign_up(credentials.model_dump()).session
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/slumby/.local/share/virtualenvs/user-api-Jy_0pb3R/lib/python3.12/site-packages/gotrue/_sync/gotrue_client.py", line 237, in sign_up
    self._notify_all_subscribers("SIGNED_IN", response.session)
  File "/home/slumby/.local/share/virtualenvs/user-api-Jy_0pb3R/lib/python3.12/site-packages/gotrue/_sync/gotrue_client.py", line 991, in _notify_all_subscribers
    subscription.callback(event, session)
  File "/home/slumby/.local/share/virtualenvs/user-api-Jy_0pb3R/lib/python3.12/site-packages/supabase/_sync/client.py", line 300, in _listen_to_auth_events
    self.realtime.set_auth(access_token)
  File "/home/slumby/.local/share/virtualenvs/user-api-Jy_0pb3R/lib/python3.12/site-packages/realtime/_sync/client.py", line 30, in wrapper
    raise NotConnectedError(func.__name__)
realtime.exceptions.NotConnectedError: A WS connection has not been established. Ensure you call RealtimeClient.connect() before calling RealtimeClient.set_auth()
Slumberdac commented 2 months ago

I tried jankily adding one line locally that runs realtime.connect() which seemed to solve the issue on supabase's side (realtime then causes an AttributeError for which i've also opened an issue) but I'm sure that this is doomed to happen to others so I come to you to see if this is in fact an accident or if it turns out my project needs to be adapted (I couldn't find anything in the documentation regarding this)

AzulGarza commented 2 months ago

i had the same problem, i think this is related to the latest releases. downgrading to supabase<2.7.0 fixed the problem.