Skip to Content
Signet

Quickstart

A runnable Signet server in about 30 lines, plus a curl smoke test.

This page gets a small Go server running with email/password sign-up and sign-in. You will need Go 1.26 or newer. For the frontend steps you will also need Node.js 20+; see Frontend client.

1. Create a module and install Signet

mkdir signet-demo && cd signet-demo
go mod init example.com/signet-demo
go get github.com/shakibdshy/signet-auth
go get modernc.org/sqlite

2. Create main.go

package main

import (
	"context"
	"crypto/rand"
	"database/sql"
	"log"
	"net/http"
	"os"
	"sync"

	"github.com/shakibdshy/signet-auth"
	"github.com/shakibdshy/signet-auth/mailer/console"
	"github.com/shakibdshy/signet-auth/store/sqlite"
	_ "modernc.org/sqlite"
)

// A minimal rate limiter for development. Use a shared store in production.
type memoryLimiter struct {
	mu   sync.Mutex
	hits map[string]int
}

func (l *memoryLimiter) Allow(_ context.Context, key string) (bool, error) {
	l.mu.Lock()
	defer l.mu.Unlock()
	l.hits[key]++
	return l.hits[key] <= 10, nil
}

func main() {
	ctx := context.Background()

	// Open SQLite and create Signet's four tables.
	db, err := sql.Open("sqlite", "file:signet.db?_pragma=foreign_keys(1)")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	store := sqlite.New(db)
	if err := store.Migrate(ctx); err != nil {
		log.Fatal(err)
	}

	// CSRF tokens are HMAC-signed; the key must be exactly 32 random bytes.
	csrfKey := make([]byte, 32)
	if _, err := rand.Read(csrfKey); err != nil {
		log.Fatal(err)
	}

	cfg := signet.DefaultConfig()
	cfg.Store = store
	cfg.Mailer = console.New(os.Stdout) // development only: prints links
	cfg.RateLimiter = &memoryLimiter{hits: make(map[string]int)}
	cfg.CSRFKey = csrfKey
	cfg.AppURL = "http://localhost:8080" // origin only, no path
	cfg.PathPrefix = "/auth"             // where the HTTP endpoints are mounted

	// Local convenience: skip email verification. Remove both lines in production.
	off := false
	cfg.RequireVerifiedEmail = &off
	cfg.SendVerificationOnSignUp = &off

	auth, err := signet.New(cfg)
	if err != nil {
		log.Fatal(err)
	}

	mux := http.NewServeMux()
	// Signet's handler expects the full /auth/... path — do not use StripPrefix.
	mux.Handle("/auth/", auth.Handler())
	mux.Handle("/private", auth.RequireAuth(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user, ok := signet.UserFromContext(r.Context())
		if !ok {
			http.Error(w, "unauthorized", http.StatusUnauthorized)
			return
		}
		_, _ = w.Write([]byte("hello " + user.Email + "\n"))
	})))

	log.Println("listening on http://localhost:8080")
	log.Fatal(http.ListenAndServe(":8080", mux))
}

Run go mod tidy && go run ..

3. Test it with curl

curl -i -X POST http://localhost:8080/auth/signup \
  -H 'Content-Type: application/json' \
  -d '{"email":"ada@example.com","password":"correct horse"}'

curl -i -X POST http://localhost:8080/auth/signin \
  -H 'Content-Type: application/json' \
  -d '{"email":"ada@example.com","password":"correct horse"}'

The sign-in response contains session_token and csrf_token and sets a session cookie. Use that cookie to access /private. State-changing HTTP requests also need X-CSRF-Token; the Frontend client sends it automatically.

What the pieces mean

Glossary in 60 seconds

A User is the person. An Account is one way they sign in (a password, Google, or a Username). A Session is one authenticated visit, identified by an opaque Session token. A Token is a single-use proof for one sensitive flow — different from a Session token. See the GLOSSARY.md.

  • Store adapts your database. Migrate creates the tables; Signet never migrates implicitly.
  • Mailer sends verification and reset links. console.New prints them to stdout for development.
  • RateLimiter decides whether a request key is allowed. You own its implementation and window.
  • CSRFKey signs CSRF tokens; AppURL is the origin used in emailed links.
  • PathPrefix says where the endpoints live. Mount auth.Handler() at that path without http.StripPrefix.

DefaultConfig() supplies secure cookie, session and password defaults. Insecure options are named Insecure* and emit warnings. See Configuration and Errors for the full reference.

Next steps

On this page