paint-brush
How to Accelerate Your Golang Backend With Supabase API by@nitoge
550 reads
550 reads

How to Accelerate Your Golang Backend With Supabase API

by lengzuoFebruary 20th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Discover how to leverage the supabase library for integrating into your Golang projects. This guide covers the installation, setup, and a simple example to get you started with using supabase for database operations and authentication in your backend, enhancing your development workflow with ease and efficiency.
featured image - How to Accelerate Your Golang Backend With Supabase API
lengzuo HackerNoon profile picture

Boost your Golang backend with the supabase API: A seamless guide to setting up and utilizing supabase for efficient database operations and authentication.

Summary

supabase has emerged as a compelling open-source alternative to Firebase, offering a suite of tools including a database, authentication, real-time subscriptions, and storage. For Golang developers, integrating it can seem daunting due to the lack of proper library support.


However, the library provides a simplified way to interact with supabase in your Golang projects. This article will guide you through setting up the library and demonstrate its use in a Golang backend project, ensuring you can leverage supabase’s powerful features with minimal hassle.

Installation

To begin, install in your Golang project.

go get github.com/lengzuo/supa

Setting Up Supabase in Your Golang Project

Before diving into the code, ensure you have setup a supabase project. From your supabase project dashboard, note down the Project Ref and the Project API keys. These will be required to connect your Golang application to supabase.

your supabase dashboard Project Settings

Configuration

Import the library along with other necessary packages in your Golang project.

package main

import (
	"fmt"
	"github.com/lengzuo/supa"
)

func main() {
    conf := supabase.Config{
        // Your project api key, you can use either `anon` or `service_role`.
        // but i will suggest you to use `service_role` as your api key and keep it secret.
		ApiKey:     "your-project-api-key",
        // Retrieve your project ref from project url
        // eg: //this-your-project-ref.supabase.co
		ProjectRef: "your-project-ref",
        // Set it `false` in production to avoid extra log print.
		Debug:      true,
	}
	supaClient, err := supabase.New(conf)
	if err != nil {
		fmt.Println("failed in init supa client: ", err)
		return
	}
}

Replace your-project-api-key and your-project-ref according from your supabase project.

Use Case

func signUp(supaClient *supabase.Client) {
  body := dto.SignUpRequest{
		Email:    "[email protected]",
		Password: "user-password",
	}
	resp, err := supaClient.Auth.SignUp(ctx, body)
    if err != nil {
        var supaErr catch.Exception
        // use this to catch the error of http status code != 2xx 
		if errors.As(err, &supaErr) {
			log.Error("status: %d, err: %s", supaErr.StatusCode(), supaErr.Error())
			return
		}
        fmt.Println("failed in sign up: ", err)
        return
    }
    bytes, _ := json.Marshal(resp)
    fmt.Printf("sign up success: %s", bytes)
}


func signInWithPassword(supaClient *supabase.Client) {
	body := dto.SignInRequest{
		Email:    "[email protected]",
		Password: "user-password",
	}
	resp, err := supaClient.Auth.SignInWithPassword(ctx, body)
    if err != nil {
       ...
    }
    bytes, _ := json.Marshal(resp)
    fmt.Printf("sign in with password success: %s", bytes)
}

func getAuthUser(supaClient *supabase.Client) {
    token = "logged-in-access-token"
	user, err := supaClient.Auth.User(ctx, token)
    if err != nil {
       ...
    }
    bytes, _ := json.Marshal(resp)
    fmt.Printf("sign in with password success: %s", bytes)
}

There are more example functions such as RPC and DB operation in the repo for your reference.

Conclusion

In my personal experience with other supabase Golang libraries, I find the library to be the most developer-friendly. It offers extensive authentication APIs such as SignInWithOTP, SignInWithOAuth, and more. Additionally, it allows developers to pass in context for each operation.


Besides that, it streamlines the process of integrating a robust backend database and authentication. With just a few lines of code, you can harness the power of supabase in your applications, making your development process more efficient and dynamic.


바카라사이트 바카라사이트 온라인바카라