Skip to content

Commit

Permalink
fix: resolve user session inside of the create factory method (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
silentworks authored Jul 16, 2024
1 parent aab9c3e commit 4635994
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
31 changes: 25 additions & 6 deletions supabase/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,21 @@ async def create(
supabase_key: str,
options: Union[ClientOptions, None] = None,
):
return cls(supabase_url, supabase_key, options)
auth_header = options.headers.get("Authorization") if options else None
client = cls(supabase_url, supabase_key, options)

if auth_header is None:
try:
session = await client.auth.get_session()
session_access_token = client._create_auth_header(session.access_token)
except Exception as err:
session_access_token = None

client.options.headers.update(
client._get_auth_headers(session_access_token)
)

return client

def table(self, table_name: str) -> AsyncRequestBuilder:
"""Perform a table operation.
Expand Down Expand Up @@ -260,13 +274,18 @@ def _init_postgrest_client(
def _create_auth_header(self, token: str):
return f"Bearer {token}"

def _get_auth_headers(self) -> Dict[str, str]:
def _get_auth_headers(
self, authorization: Union[str, None] = None
) -> Dict[str, str]:
if authorization is None:
authorization = self.options.headers.get(
"Authorization", self._create_auth_header(self.supabase_key)
)

"""Helper method to get auth headers."""
return {
"apiKey": self.supabase_key,
"Authorization": self.options.headers.get(
"Authorization", self._create_auth_header(self.supabase_key)
),
"Authorization": authorization,
}

def _listen_to_auth_events(
Expand Down Expand Up @@ -314,6 +333,6 @@ async def create_client(
-------
Client
"""
return AsyncClient(
return AsyncClient.create(
supabase_url=supabase_url, supabase_key=supabase_key, options=options
)
31 changes: 25 additions & 6 deletions supabase/_sync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,21 @@ def create(
supabase_key: str,
options: Union[ClientOptions, None] = None,
):
return cls(supabase_url, supabase_key, options)
auth_header = options.headers.get("Authorization") if options else None
client = cls(supabase_url, supabase_key, options)

if auth_header is None:
try:
session = client.auth.get_session()
session_access_token = client._create_auth_header(session.access_token)
except Exception as err:
session_access_token = None

client.options.headers.update(
client._get_auth_headers(session_access_token)
)

return client

def table(self, table_name: str) -> SyncRequestBuilder:
"""Perform a table operation.
Expand Down Expand Up @@ -260,13 +274,18 @@ def _init_postgrest_client(
def _create_auth_header(self, token: str):
return f"Bearer {token}"

def _get_auth_headers(self) -> Dict[str, str]:
def _get_auth_headers(
self, authorization: Union[str, None] = None
) -> Dict[str, str]:
if authorization is None:
authorization = self.options.headers.get(
"Authorization", self._create_auth_header(self.supabase_key)
)

"""Helper method to get auth headers."""
return {
"apiKey": self.supabase_key,
"Authorization": self.options.headers.get(
"Authorization", self._create_auth_header(self.supabase_key)
),
"Authorization": authorization,
}

def _listen_to_auth_events(
Expand Down Expand Up @@ -314,6 +333,6 @@ def create_client(
-------
Client
"""
return SyncClient(
return SyncClient.create(
supabase_url=supabase_url, supabase_key=supabase_key, options=options
)

0 comments on commit 4635994

Please sign in to comment.