App Logo
Get Started

Basic Usage

main.go
package main

import (
	"os"
	"log"
	"net/http"

	gobetterauth "github.com/GoBetterAuth/go-better-auth"
	gobetterauthconfig "github.com/GoBetterAuth/go-better-auth/config"
	gobetterauthmodels "github.com/GoBetterAuth/go-better-auth/models"
)

func main() {
	config := gobetterauthconfig.NewConfig(
		gobetterauthconfig.WithAppName("YourAppName"),
		gobetterauthconfig.WithDatabase(gobetterauthmodels.DatabaseConfig{
			Provider:         "postgres",
			ConnectionString: os.Getenv("DATABASE_URL"),
		}),
		gobetterauthconfig.WithEmailPassword(gobetterauthmodels.EmailPasswordConfig{
			Enabled: true,
		}),
	)
	auth := gobetterauth.New(config)
	auth.RunMigrations()

	// Mount auth routes
	http.Handle("/auth/", auth.Handler())

	// Add your application routes
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Welcome to My App!"))
	})

	// Start the server
	log.Println("Server running on http://localhost:8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Testing the Setup

Sign Up

curl -X POST http://localhost:8080/auth/sign-up/email \
  -H "Content-Type: application/json" \
  -d '{"name": "John Doe", "email": "john.doe@example.com", "password": "password123"}'

Sign In

curl -X POST http://localhost:8080/auth/sign-in/email \
  -H "Content-Type: application/json" \
  -d '{"email": "john.doe@example.com", "password": "password123"}'

Get Session

curl -X GET http://localhost:8080/auth/me \
  -H "Cookie: gobetterauth.session_token=your-session-token"

On this page