magiclink

package
v2.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package magiclink owns the request / consume flow for email-delivered magic-link sign-in tokens. Extracted from root service_magiclink.go in PR A of the 2026-06 architecture reorg.

On Request the package mints a fresh token, persists its hash with a TTL, and emails the raw token to the user as a click-through link. On Consume the package atomically marks the link used, finds-or-creates the user, marks email verified, and issues a fresh session via the injected session.Issuer (so this package does not need to depend on the session package directly; tests inject a stub).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service holds the dependencies needed to request and consume magic links. Constructed once in theauth.New.

func New

func New(storage Storage, sender email.Sender, baseURL string, ttl time.Duration, sessions SessionIssuer, em audit.Emitter) *Service

New constructs a magiclink Service.

func (*Service) Consume

func (s *Service) Consume(ctx context.Context, token string) (sessionToken string, user *models.User, created bool, err error)

Consume atomically marks a magic-link as used, finds-or-creates the corresponding user, marks their email verified, and issues a fresh session. Returns models.ErrInvalidToken for unknown tokens and models.ErrMagicLinkExpired when the link's TTL has elapsed. The created return reports whether the user row was newly created during this call (v2.5: lets the root forwarder distinguish OnSignup from OnSignin dispatch).

func (*Service) Request

func (s *Service) Request(ctx context.Context, emailAddr string) error

Request mints a magic-link token, persists its hash, and emails the raw token to the user as a click-through verification link. Production code calls this; the raw token only ever appears in the inbox.

func (*Service) RequestForTest

func (s *Service) RequestForTest(ctx context.Context, emailAddr string) (string, error)

RequestForTest is the same as Request but returns the raw token so tests can drive a consume flow without scraping email.

func (*Service) SetHardening

func (s *Service) SetHardening(norm emailnorm.Normalizer, gate SignupGate)

SetHardening configures email canonicalization and the signup gate. Call before the service handles requests; gate may be nil.

func (*Service) SetPathPrefix

func (s *Service) SetPathPrefix(p string)

SetPathPrefix sets the route prefix used in emailed links. Empty keeps the default.

type SessionIssuer

type SessionIssuer interface {
	Issue(ctx context.Context, user models.User, userAgent, ip string) (string, models.Session, error)
}

SessionIssuer abstracts the session.Issue call. Allows magiclink to mint a session at consume time without importing internal/session (the root constructor wires the concrete *session.Service in).

type SignupGate

type SignupGate interface {
	Begin(ctx context.Context) (done func(created *models.User), err error)
}

SignupGate decides whether a magic link may create a new user.

type Storage

type Storage interface {
	CreateMagicLink(ctx context.Context, ml models.MagicLink) error
	ConsumeMagicLink(ctx context.Context, tokenHash []byte) (*models.MagicLink, error)
	UserByEmail(ctx context.Context, email string) (*models.User, error)
	CreateUser(ctx context.Context, u models.User) (models.User, error)
	MarkEmailVerified(ctx context.Context, userID models.ULID) error
}

Storage is the minimal persistence subset this package needs. Declared here (not imported from root) so internal/magiclink does not import the root theauth package and the constructor cycle stays broken.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL