Skip to content

Commit 4635994

Browse files
authored
fix: resolve user session inside of the create factory method (#851)
1 parent aab9c3e commit 4635994

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

supabase/_async/client.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,21 @@ async def create(
9898
supabase_key: str,
9999
options: Union[ClientOptions, None] = None,
100100
):
101-
return cls(supabase_url, supabase_key, options)
101+
auth_header = options.headers.get("Authorization") if options else None
102+
client = cls(supabase_url, supabase_key, options)
103+
104+
if auth_header is None:
105+
try:
106+
session = await client.auth.get_session()
107+
session_access_token = client._create_auth_header(session.access_token)
108+
except Exception as err:
109+
session_access_token = None
110+
111+
client.options.headers.update(
112+
client._get_auth_headers(session_access_token)
113+
)
114+
115+
return client
102116

103117
def table(self, table_name: str) -> AsyncRequestBuilder:
104118
"""Perform a table operation.
@@ -260,13 +274,18 @@ def _init_postgrest_client(
260274
def _create_auth_header(self, token: str):
261275
return f"Bearer {token}"
262276

263-
def _get_auth_headers(self) -> Dict[str, str]:
277+
def _get_auth_headers(
278+
self, authorization: Union[str, None] = None
279+
) -> Dict[str, str]:
280+
if authorization is None:
281+
authorization = self.options.headers.get(
282+
"Authorization", self._create_auth_header(self.supabase_key)
283+
)
284+
264285
"""Helper method to get auth headers."""
265286
return {
266287
"apiKey": self.supabase_key,
267-
"Authorization": self.options.headers.get(
268-
"Authorization", self._create_auth_header(self.supabase_key)
269-
),
288+
"Authorization": authorization,
270289
}
271290

272291
def _listen_to_auth_events(
@@ -314,6 +333,6 @@ async def create_client(
314333
-------
315334
Client
316335
"""
317-
return AsyncClient(
336+
return AsyncClient.create(
318337
supabase_url=supabase_url, supabase_key=supabase_key, options=options
319338
)

supabase/_sync/client.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,21 @@ def create(
9898
supabase_key: str,
9999
options: Union[ClientOptions, None] = None,
100100
):
101-
return cls(supabase_url, supabase_key, options)
101+
auth_header = options.headers.get("Authorization") if options else None
102+
client = cls(supabase_url, supabase_key, options)
103+
104+
if auth_header is None:
105+
try:
106+
session = client.auth.get_session()
107+
session_access_token = client._create_auth_header(session.access_token)
108+
except Exception as err:
109+
session_access_token = None
110+
111+
client.options.headers.update(
112+
client._get_auth_headers(session_access_token)
113+
)
114+
115+
return client
102116

103117
def table(self, table_name: str) -> SyncRequestBuilder:
104118
"""Perform a table operation.
@@ -260,13 +274,18 @@ def _init_postgrest_client(
260274
def _create_auth_header(self, token: str):
261275
return f"Bearer {token}"
262276

263-
def _get_auth_headers(self) -> Dict[str, str]:
277+
def _get_auth_headers(
278+
self, authorization: Union[str, None] = None
279+
) -> Dict[str, str]:
280+
if authorization is None:
281+
authorization = self.options.headers.get(
282+
"Authorization", self._create_auth_header(self.supabase_key)
283+
)
284+
264285
"""Helper method to get auth headers."""
265286
return {
266287
"apiKey": self.supabase_key,
267-
"Authorization": self.options.headers.get(
268-
"Authorization", self._create_auth_header(self.supabase_key)
269-
),
288+
"Authorization": authorization,
270289
}
271290

272291
def _listen_to_auth_events(
@@ -314,6 +333,6 @@ def create_client(
314333
-------
315334
Client
316335
"""
317-
return SyncClient(
336+
return SyncClient.create(
318337
supabase_url=supabase_url, supabase_key=supabase_key, options=options
319338
)

0 commit comments

Comments
 (0)