11import re
22from typing import Any , Dict , Union
33
4+ from gotrue import SyncMemoryStorage
45from gotrue .types import AuthChangeEvent , Session
56from httpx import Timeout
67from postgrest import SyncFilterRequestBuilder , SyncPostgrestClient , SyncRequestBuilder
@@ -27,7 +28,7 @@ def __init__(
2728 self ,
2829 supabase_url : str ,
2930 supabase_key : str ,
30- options : ClientOptions = ClientOptions (),
31+ options : ClientOptions = ClientOptions (storage = SyncMemoryStorage () ),
3132 ):
3233 """Instantiate the client.
3334
@@ -235,6 +236,11 @@ def _init_postgrest_client(
235236 rest_url , headers = headers , schema = schema , timeout = timeout
236237 )
237238
239+ def _create_auth_header (self , token : str ):
240+ return {
241+ "Authorization" : f"Bearer { token } " ,
242+ }
243+
238244 def _get_auth_headers (self ) -> Dict [str , str ]:
239245 """Helper method to get auth headers."""
240246 return {
@@ -249,22 +255,26 @@ def _get_token_header(self):
249255 except Exception as err :
250256 access_token = self .supabase_key
251257
252- return {
253- "Authorization" : f"Bearer { access_token } " ,
254- }
258+ return self ._create_auth_header (access_token )
255259
256- def _listen_to_auth_events (self , event : AuthChangeEvent , session : Session ):
260+ def _listen_to_auth_events (
261+ self , event : AuthChangeEvent , session : Union [Session , None ]
262+ ):
263+ access_token = self .supabase_key
257264 if event in ["SIGNED_IN" , "TOKEN_REFRESHED" , "SIGNED_OUT" ]:
258265 # reset postgrest and storage instance on event change
259266 self ._postgrest = None
260267 self ._storage = None
261268 self ._functions = None
269+ access_token = session .access_token if session else self .supabase_key
270+
271+ self ._auth_token = self ._create_auth_header (access_token )
262272
263273
264274def create_client (
265275 supabase_url : str ,
266276 supabase_key : str ,
267- options : ClientOptions = ClientOptions (),
277+ options : ClientOptions = ClientOptions (storage = SyncMemoryStorage () ),
268278) -> SyncClient :
269279 """Create client function to instantiate supabase client like JS runtime.
270280
0 commit comments