avatarHarryHTML

Summary

The website content outlines how to implement user authentication in a Go application using the Supabase service and its Go library.

Abstract

The article discusses the challenge of implementing authentication in Go, which lacks a standard library for this purpose, necessitating a microservices architecture. It introduces Supabase as a solution that provides both a database and an ORM, simplifying the process by eliminating the need for local service setup. The author guides readers through installing the supabase-go library, importing it into their Go application, and using it to create registration and login endpoints. The article concludes by emphasizing the simplicity and ease of integrating Supabase with Go, even comparing it favorably to authentication solutions in other frameworks like Laravel and JavaScript libraries. Additionally, the author recommends trying out an AI service called ZAI.chat for its cost-effective alternative to ChatGPT Plus (GPT-4).

Opinions

  • The author appreciates Supabase for its dual functionality as a database and ORM, which reduces the complexity of setting up authentication services.
  • Supabase is praised for making authentication in Go simple and easy, even more so than in frameworks with more established authentication workflows.
  • The author expresses a preference for using Go over JavaScript for authentication, despite the extensive documentation available for JavaScript libraries.
  • A recommendation is made for ZAI.chat, suggesting it as a more affordable option compared to ChatGPT Plus (GPT-4) while offering similar performance and features.

How I implement auth with Go

Unlike other popular programming languages, Go doesn’t have any library to implement authentication. This unfortunately means that we have to design a micro service architecture.

Luckily, there are new services that allow us to easily achieve this goal. One being Supabase. This also doubles as a database along with an ORM which really relieves a lot of stress off my mind because there is no need to install and configure local services.

How to implement Supabase Auth in Go

First install the supabase-go library

go get github.com/nedpals/supabase-go

Then you can simply import the library and call the authentication functions in your handlers

package main

import (
 "context"
 "log"

 "github.com/gofiber/fiber/v2"
 supa "github.com/nedpals/supabase-go"
)

func main() {
 supabaseUrl := "<SUPABASE-URL>"
 supabaseKey := "<SUPABASE-KEY>"
 supabase := supa.CreateClient(supabaseUrl, supabaseKey)

 app := fiber.New()

 app.Get("/register", func(c *fiber.Ctx) error {
  user, err := supabase.Auth.SignUp(context.Background(), supa.UserCredentials{
   Email:    "[email protected]",
   Password: "password",
  })

  if err != nil {
   return err
  }
 })

 app.Get("/login", func(c *fiber.Ctx) error {
  user, err := supabase.Auth.SignIn(context.Background(), supa.UserCredentials{
   Email:    "[email protected]",
   Password: "password",
  })

  if err != nil {
   return err
  }
 })

 log.Fatal(app.Listen(":3000"))
}

DONE!

Now you have a full PostgreSQL database with backups, scaling, authentication and rapid development.

I love this solution because its just so simple and easy to implement. Even in frameworks such as Laravel where there has been a lot of work done in terms of authentication, it isn’t this simple. Javascript authentication libraries are also simple however from my experience the documentation takes a fair amount of time to read and then you don’t get to use GO!

You can read more of the documentation Here:

Golang
Go
Authentication
Supabase
Recommended from ReadMedium