supabase-community / supabase-go

A Go Client library for Supabase
MIT License
226 stars 19 forks source link

Auth is not intialised with User Token #16

Open hemanth-gopi opened 4 months ago

hemanth-gopi commented 4 months ago

Auth is not initialised with User Token

Whenever you create a new Client using supabase.NewClient() and by passing custom headers with the Authorisation token, the Auth field is not initialized with the same token.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

Initialize the supabase Client as follows with the user token

headers := map[string]string{
    "Authorization": token,
}

clientOptions := supabase.ClientOptions{
    Headers: headers,
    Schema:  "custom_schema",
}

client, err := supabase.NewClient(supabaseApiUrl, supabaseAnonKey, &clientOptions)

Now when you want to access the User object, you intend to use the following snippet user, err := client.Auth.GetUser()

But this will lead to the following error

"msg":"error getting user",
"error":"response status code 401: 
{
    \"code\":401,
    \"error_code\":\"no_authorization\",
    \"msg\":\"This endpoint requires a Bearer token\"
}

Expected behavior

A user object should be obtained with user details using the token passed during initialization of superbase client

System information

Additional context

Add any other context about the problem here.

0x113 commented 4 months ago

Facing the same issue. Have you every found a fix for that?

hemanth-gopi commented 4 months ago

There is a client method called updateAuthSession. You can use that to update the token and authenticate the fields inside the client

// Validate the token
session := types.Session{}
session.AccessToken = token
client.UpdateAuthSession(session)
0x113 commented 4 months ago

Works like a charm! Thanks! They really should update the documentation.

dennj commented 3 days ago

I have the same problem. The workaround is working.