Skip to content

failed to get_session after create_client by accees_token as supabase_key #663

@AtticusZeller

Description

@AtticusZeller

Describe the bug

# access_token from AuthResponse.session.access_token  by sign in from somewhere
async def get_db(access_token: AccessTokenDep) -> AsyncClient:
    client: AsyncClient | None = None
    try:
        client = await create_client(
            settings.SUPABASE_URL,
            access_token,
            options=ClientOptions(
                postgrest_client_timeout=10, storage_client_timeout=10
            ),
        )
        session = await client.auth.get_session()
        # client.postgrest.auth(token=access_token)
        user = await client.auth.get_user()
        yield client
    except Exception as e:
        logging.error(e)
        raise HTTPException(status_code=401, detail=e)
    finally:
        if client:
            await client.auth.sign_out()

session = await client.auth.get_session(),session got None unless i signed in with password etc

in short ,it should be able to recognize the access token from front_end after signed in , create_client with the access token as supabase_key should work for it

To Reproduce
just called

async def create_client(
    supabase_url: str,
    supabase_key: str,
    options: ClientOptions = ClientOptions(),
) -> AsyncClient:
    ....
    return await AsyncClient.create(
        supabase_url=supabase_url, supabase_key=supabase_key, options=options
    )
@classmethod
    async def create(
        cls,
        supabase_url: str,
        supabase_key: str,
        options: ClientOptions = ClientOptions(),
    ):
        client = cls(supabase_url, supabase_key, options)
        client._auth_token = await client._get_token_header()
        return client

add break point at client._auth_token = await client._get_token_header()
you will find that client._auth_token set to None!!!,which means the @property def postgrest(self): can not be inited correctly by access_token

self._auth_token = {
            "Authorization": f"Bearer {supabase_key}",
        }
``

**Expected behavior**
```python
    async def _get_token_header(self):
        try:
            session = await self.auth.get_session()
            access_token = session.access_token
        except Exception as err:
            access_token = self.supabase_key

        return self._create_auth_header(access_token)

client._auth_token = await client._get_token_header()
the first time after called get_session() should return the correct session like client.auth.get_user(jwt),it works

Desktop (please complete the following information):

  • OS: win
  • Version v 2.3.3

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions