Skip to content

Commit

Permalink
Migrate to auth-go from gotrue-go
Browse files Browse the repository at this point in the history
Related to supabase-community#24

Update references from `gotrue-go` to `auth-go` in the codebase.

* **client.go**
  - Update import paths to reference `auth-go` instead of `gotrue-go`.
  - Update the `Client` struct to use the `auth-go` client.
  - Update the `NewClient` function to initialize the `auth-go` client.
  - Update the `SignInWithEmailPassword`, `SignInWithPhonePassword`, `EnableTokenAutoRefresh`, `RefreshToken`, and `UpdateAuthSession` methods to use `auth-go` types and methods.

* **client_test.go**
  - Update import paths to reference `auth-go` instead of `gotrue-go`.
  - Update the `NewClient` function calls to initialize the `auth-go` client.
  - Update the test functions (`TestFrom`, `TestRpc`, `TestStorage`, and `TestFunctions`) to use `auth-go` types and methods.

* **go.mod**
  - Remove `gotrue-go` dependency.
  - Add `auth-go` dependency.

* **README.md**
  - Update the features section to mention `auth-go` instead of `gotrue-go`.

* **test/remote_client.go**
  - Update import paths to reference `auth-go` instead of `gotrue-go`.
  - Update the `NewClient` function call to initialize the `auth-go` client.
  - Update the `SignInWithEmailPassword` method call to use `auth-go` types and methods.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/supabase-community/supabase-go/issues/24?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
4cecoder committed Oct 24, 2024
1 parent ea99d95 commit ae0bf6e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ An isomorphic Go client for Supabase.
- Realtime listeners for database changes
- [x] Integration with [Postgrest](https://github.com/supabase-community/postgrest-go)
- Access your database using a REST API generated from your schema & database functions
- [x] Integration with [Gotrue](https://github.com/supabase-community/gotrue-go)
- [x] Integration with [Auth](https://github.com/supabase-community/auth-go)
- User authentication, including OAuth, ***email/password***, and native sign-in
- [x] Integration with [Supabase Storage](https://github.com/supabase-community/storage-go)
- Store files in S3 with additional managed metadata
Expand Down
17 changes: 5 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"log"
"time"

"github.com/supabase-community/auth-go"
"github.com/supabase-community/auth-go/types"
"github.com/supabase-community/functions-go"
"github.com/supabase-community/gotrue-go"
"github.com/supabase-community/gotrue-go/types"
postgrest "github.com/supabase-community/postgrest-go"
storage_go "github.com/supabase-community/storage-go"
)
Expand All @@ -20,11 +20,9 @@ const (
)

type Client struct {
// Why is this a private field??
rest *postgrest.Client
Storage *storage_go.Client
// Auth is an interface. We don't need a pointer to an interface.
Auth gotrue.Client
Auth auth.Client
Functions *functions.Client
options clientOptions
}
Expand Down Expand Up @@ -62,7 +60,6 @@ func NewClient(url, key string, options *ClientOptions) (*Client, error) {

client := &Client{}
client.options.url = url
// map is pass by reference, so this gets updated by rest of function
client.options.headers = headers

var schema string
Expand All @@ -74,9 +71,8 @@ func NewClient(url, key string, options *ClientOptions) (*Client, error) {

client.rest = postgrest.NewClient(url+REST_URL, schema, headers)
client.Storage = storage_go.NewClient(url+STORAGE_URL, key, headers)
// ugly to make auth client use custom URL
tmp := gotrue.New(url, key)
client.Auth = tmp.WithCustomGoTrueURL(url + AUTH_URL)
tmp := auth.New(url, key)
client.Auth = tmp.WithCustomAuthURL(url + AUTH_URL)
client.Functions = functions.NewClient(url+FUNCTIONS_URL, key, headers)

return client, nil
Expand Down Expand Up @@ -124,7 +120,6 @@ func (c *Client) EnableTokenAutoRefresh(session types.Session) {
time.Sleep(sleepDuration)
}

// Refresh the token
newSession, err := c.RefreshToken(session.RefreshToken)
if err != nil {
attempt++
Expand All @@ -138,7 +133,6 @@ func (c *Client) EnableTokenAutoRefresh(session types.Session) {
continue
}

// Update the session, reset the attempt counter, and update the expiresAt time
c.UpdateAuthSession(newSession)
session = newSession
attempt = 0
Expand All @@ -162,5 +156,4 @@ func (c *Client) UpdateAuthSession(session types.Session) {
c.options.headers["Authorization"] = "Bearer " + session.AccessToken
c.Storage = storage_go.NewClient(c.options.url+STORAGE_URL, session.AccessToken, c.options.headers)
c.Functions = functions.NewClient(c.options.url+FUNCTIONS_URL, session.AccessToken, c.options.headers)

}
10 changes: 5 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/supabase-community/supabase-go"
"github.com/supabase-community/auth-go"
)

const (
Expand All @@ -13,7 +13,7 @@ const (
)

func TestFrom(t *testing.T) {
client, err := supabase.NewClient(API_URL, API_KEY, nil)
client, err := auth.NewClient(API_URL, API_KEY, nil)
if err != nil {
fmt.Println("cannot initalize client", err)
}
Expand All @@ -22,7 +22,7 @@ func TestFrom(t *testing.T) {
}

func TestRpc(t *testing.T) {
client, err := supabase.NewClient(API_URL, API_KEY, nil)
client, err := auth.NewClient(API_URL, API_KEY, nil)
if err != nil {
fmt.Println("cannot initalize client", err)
}
Expand All @@ -31,7 +31,7 @@ func TestRpc(t *testing.T) {
}

func TestStorage(t *testing.T) {
client, err := supabase.NewClient(API_URL, API_KEY, nil)
client, err := auth.NewClient(API_URL, API_KEY, nil)
if err != nil {
fmt.Println("cannot initalize client", err)
}
Expand All @@ -40,7 +40,7 @@ func TestStorage(t *testing.T) {
}

func TestFunctions(t *testing.T) {
client, err := supabase.NewClient(API_URL, API_KEY, nil)
client, err := auth.NewClient(API_URL, API_KEY, nil)
if err != nil {
fmt.Println("cannot initalize client", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ toolchain go1.22.1

require (
github.com/joho/godotenv v1.5.1
github.com/supabase-community/auth-go v1.0.0
github.com/supabase-community/functions-go v0.0.0-20220927045802-22373e6cb51d
github.com/supabase-community/gotrue-go v1.2.0
github.com/supabase-community/postgrest-go v0.0.11
github.com/supabase-community/storage-go v0.7.0
)
Expand Down
10 changes: 2 additions & 8 deletions test/remote_client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// This is basic example for postgrest-go library usage.
// For now this example is represent wanted syntax and bindings for library.
// After core development this test files will be used for CI tests.

package main

import (
Expand All @@ -10,7 +6,7 @@ import (
"os"

"github.com/joho/godotenv"
"github.com/supabase-community/supabase-go"
"github.com/supabase-community/auth-go"
)

func main() {
Expand All @@ -24,17 +20,15 @@ func main() {
email := os.Getenv("TESTUSER")
password := os.Getenv("TESTUSERPASSWORD")

client, err := supabase.NewClient(projectURL, anonKey, nil)
client, err := auth.NewClient(projectURL, anonKey, nil)
if err != nil {
fmt.Println("cannot initalize client", err)
}
client.SignInWithEmailPassword(email, password)

//
rooms, _, err := client.From("rooms").Select("*", "", false).ExecuteString()
if err != nil {
panic(err)
}
fmt.Println(rooms)

}

0 comments on commit ae0bf6e

Please sign in to comment.