Skip to main content

Adapters overview

An adapter implements goauth.Adapter — the persistence layer Auth.js calls “database adapters”.

Core methods

MethodPurpose
CreateUser / GetUser / GetUserByEmailUser records
LinkAccount / GetUserByAccountOAuth account linking
CreateSession / GetSessionAndUser / DeleteSessionDatabase sessions
CreateVerificationToken / UseVerificationTokenMagic links, OTP, MFA, passkey challenges
DeleteVerificationTokensByIdentifierPurge prior codes before MFA/OTP resend (called by core)

Bundled adapters

PackageBackendMigratorSessionListerAuthenticatorStore
adapters/postgresPostgreSQLYesYesYes
adapters/mysqlMySQL / MariaDBYesYesYes
adapters/mariadbAlias of mysqlYesYesYes
adapters/memoryIn-process mapsNoYesYes
adapters/redisRedisYesNo
adapters/mongodbMongoDBYesNo

Simple usage

import (
"database/sql"
_ "github.com/lib/pq"
"github.com/izetmolla/goauth/adapters/postgres"
)

db, _ := sql.Open("postgres", dsn)
adapter := postgres.New(db)

auth, _ := goauth.New(goauth.Config{
Secret: []string{secret},
Adapter: adapter, // auto Migrate() on New if Migrator
})

GORM

Pass GORM’s SQL pool:

sqlDB, _ := gormDB.DB()
adapter := postgres.New(sqlDB)

See SQL adapters.

Verification tokens & resend

MFA and email/OTP flows store one-time codes in verification_tokens. Before issuing a new code, goauth calls DeleteVerificationTokensByIdentifier so:

  • Re-login or resend does not hit duplicate-key errors in Postgres/MySQL
  • Only the latest code is valid for that identifier (mfa:{userId}, email, or phone)

Custom adapters must implement this method. All bundled adapters (memory, postgres, mysql, mariadb, mongodb, redis) do.

// Example for a custom SQL adapter
func (a *Adapter) DeleteVerificationTokensByIdentifier(ctx context.Context, identifier string) error {
_, err := a.db.ExecContext(ctx,
`DELETE FROM verification_tokens WHERE identifier = $1`, identifier)
return err
}