Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use provided JWT to make supabase client object logged with this JWT #70

Closed
karolzlot opened this issue Oct 17, 2021 · 6 comments · Fixed by supabase-community/auth-py#23

Comments

@karolzlot
Copy link

Is your feature request related to a problem? Please describe.

I would like to have this feature: supabase.auth.setAuth(jwt);

supabase/supabase#1094 (reply in thread)

Currently I think I can just use JWT in create_client(url, key) and effect will be the same? (I'm not sure)

Describe the solution you'd like
above

Describe alternatives you've considered
n/a

Additional context
n/a

@leynier
Copy link
Member

leynier commented Oct 17, 2021

I'm currently working on a PR to add dataclasses to the responses and synchronize the functionalities between gotrue-py and gotrue-js, that functionality that you mention is already in gotrue-js so I will add it to gotrue-py in the PR.

supabase-community/auth-py#23

@leynier
Copy link
Member

leynier commented Oct 17, 2021

When I say, "currently", it is literal 😅. Here you have a preview

def set_auth(self, access_token: str) -> Session:
    """Overrides the JWT on the current client. The JWT will then be sent in
    all subsequent network requests.

    Parameters
    ----------
    access_token : str
        A JWT token

    Returns
    -------
    response : Session
        A logged-in session

    Raises
    ------
    error : ApiError
        If an error occurs
    """
    session = Session(
        access_token=access_token,
        token_type="bearer",
        user=None,
        expires_in=None,
        expires_at=None,
        refresh_token=None,
        provider_token=None,
    )
    if self.current_session:
        session.expires_in = self.current_session.expires_in
        session.expires_at = self.current_session.expires_at
        session.refresh_token = self.current_session.refresh_token
        session.provider_token = self.current_session.provider_token
    self._save_session(session)
    return session

@karolzlot
Copy link
Author

Ok thank you!

Is it correct that the effect is the same in two snippets below??

supabase_client = create_client(url, anon_key)
supabase_client.set_auth(user_jwt_key)
supabase_client = create_client(url, user_jwt_key)

@leynier
Copy link
Member

leynier commented Oct 17, 2021

If I'm not mistaken (because I'm familiar with gotrue, but not much with the other libraries) they don't do the same.

By the way, what you want to make in the 1st option is currently possible. Although I do not know if a new release has been published with that change.

supabase_client = create_client(url, anon_key)
supabase_client.auth.set_auth(user_jwt_key)

https://github.com/supabase-community/gotrue-py/blob/9ba3192dbdccd2f02a4819b52dd6cf51095af7e7/gotrue/client.py#L162

@0xDebabrata
Copy link

Hey, not sure if I'm doing anything wrong. But the following does not seem to work.

session = supabase.auth.sign_in(email=os.environ.get("EMAIL"), password=os.environ.get("PASS"))

supabase.auth.set_auth(access_token=session.access_token)

data = supabase.table("videos").select("*").execute()
print(len(data.data))

This prints 0 normally but if I create the supabase client with the secret key, it prints the total number of rows. The table has RLS enabled.

@Antony-Jia
Copy link

from supabase import create_client, Client
The Client has no functions called set_auth.
Is that right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@karolzlot @leynier @0xDebabrata @Antony-Jia and others