go-auth

Installation

Prerequisites, what you need before configuring go-auth, and a map of the configuration pattern.

Installation

Prerequisites

  • Go 1.26 or later
  • A database: PostgreSQL, MySQL, or SQLite
  • An SMTP server, or your own mailer implementation: required unless every email-sending feature is off (invites, email verification, 2FA, and admin login's two-factor challenge, the last of which is on by default and needs TwoFactorConfig.DisableAdminTwoFactor to turn off). EnvironmentDev is the one exception when a mailer is otherwise needed: with neither WithMailer nor WithEmail set, it falls back to a log-only driver. See Configuration.

Install

go get github.com/nazimdjebloun/go-auth

go-auth's own go.mod requires pgx/v5 (PostgreSQL) and modernc.org/sqlite (SQLite) directly, plus the libraries it uses. The library imports the pgx stdlib driver itself, so PostgreSQL is registered automatically. For SQLite or MySQL, your program must blank-import the driver package it uses so it registers with database/sql:

DatabasePackage
PostgreSQLgithub.com/jackc/pgx/v5/stdlib
SQLitemodernc.org/sqlite (pure Go, no CGO)
MySQLgithub.com/go-sql-driver/mysql

If the driver you configured is not actually registered (the import is missing), New() fails fast at startup with an error naming the missing import. It does not fail later on the first query.

The goauth CLI

Schema and bootstrap tooling, shipped alongside the library. There are two ways to run it, and they do the same thing:

# Install once, then call it by name
go install github.com/nazimdjebloun/go-auth/cmd/goauth@latest
goauth migrate --driver postgres --dsn "$DATABASE_URL"

# Or run it without installing anything
go run github.com/nazimdjebloun/go-auth/cmd/goauth@latest migrate \
  --driver postgres --dsn "$DATABASE_URL"

go install is shorter to type afterwards and faster on repeat use, but it needs $GOBIN (or $GOPATH/bin) on your PATH, and it pins whatever version you installed until you reinstall. go run leaves nothing behind and is always current. Use it for CI, a Dockerfile, or a one-off bootstrap, which is how migrate and seed-admin are usually used.

`@latest` is required, in both forms

cmd/goauth is a separate module with its own go.mod, so it isn't part of your project's dependency graph even after go get of the library. Drop the version suffix and Go looks for the package among your own dependencies and fails with no required module provides package. This is not a run vs install distinction: both need it. Pin a release instead of @latest if you want reproducible builds.

CommandWhat it does
migrateConnects to your database and applies the schema directly, one statement at a time. Requires --driver and --dsn.
generateWrites the schema to a .sql file instead of applying it: check it in, or feed it to Atlas, golang-migrate, Flyway, or your own migration tool. Requires --driver; --out defaults to auth.schema.sql.
seed-adminCreates the first admin account directly in the database, since every admin route needs an existing admin session to call. Sends a real email first and writes nothing if that send fails. See Admin → Creating the first admin.

Each command takes --help. Schemas covers migrate and generate in full.

What you'll need before configuring

Gather these before writing your configuration:

  • A base URL for your frontend: used to build links in emails (verification, invites, password reset).
  • A database connection: either a connection string, or a database/pool you already opened yourself.
  • A signing secret: at least 32 random bytes. This is the one piece of key material the library needs; source it from your environment, never commit it.
  • The origins your frontend is served from: used for CSRF origin checking. No wildcard option exists.
  • A mailer, conditionally: required if you enable invite-only signup, email verification, or 2FA, or leave admin login's two-factor challenge on (the default; turn it off with TwoFactorConfig.DisableAdminTwoFactor for a mailer-free, API-only deployment). Either SMTP credentials or your own delivery implementation (Resend, Postmark, SES, etc.) work. EnvironmentDev alone falls back to a log-only driver when a mailer is needed but neither is configured.
  • OAuth credentials, conditionally: a client ID, client secret, and redirect URL per provider, only if you want OAuth login.

Nothing here is read from the environment by the library itself. go-auth has no notion of env vars. You decide how your program sources these values (environment variables are the usual choice) and pass them in as plain Go values.

The configuration pattern, at a glance

Pass option functions to NewConfig(opts ...Option). The table maps each option to its responsibility; Configuration documents every field and validation rule.

OptionConfigures
WithAppApp name, base URL, database connection, deployment environment
WithSecretThe root secret for CSRF, OAuth, 2FA, and OTP keys
WithSecurityAllowed origins, password policy, CSRF token
WithSessionSession, refresh-token, and verification/reset-token lifetimes
WithTwoFactorEmail two-factor settings
WithCookieCookie name, domain, path, SameSite, Secure
WithRegistrationWhich signup methods are available
WithOrganizationsMulti-tenant organizations
WithMailerA custom mailer implementation
WithEmailThe built-in SMTP mailer
WithTemplatesCustom email templates
WithPasswordHasherA self-identifying password hasher, including the built-in Argon2id implementation
WithBcryptCostKeep bcrypt and change its cost
WithPasswordPepperConfigure opt-in, versioned password peppering and rotation keys
WithRateLimit and its narrower variantsPer-route rate limiting
WithProviderRegister an OAuth provider
WithLoggerStructured logging
WithAudit / WithAuditSinkAudit logging

Database setup

The schema ships embedded in the library. There is no .sql file to find on disk after go get. Apply it once before starting your app for the first time:

go run github.com/nazimdjebloun/go-auth/cmd/goauth@latest migrate \
  --driver postgres --dsn "$DATABASE_URL"

Schemas covers the tables themselves, the other two ways to get at the schema, and what to expect when it changes between releases.

Next

On this page