web/auth package provides the JSON login and logout handlers, plus MFA,
magic-link, and passkey HTTP flows. Every handler is built the same way:
construct a *Config struct and pass it to a constructor function that
returns an http.Handler. There are no functional options (WithX(...)) —
all configuration is struct fields, with a Manager/Store (or equivalent)
required and everything else defaulted.
Import
Login handler
The internal sequence — including the session-fixation protection folded in after credential validation:LoginConfig.Manager and .Store are required — LoginHandler panics at
construction time if either is nil. Session-fixation protection runs on every
successful login (a pre-auth session is migrated to a fresh id, never reused).
Request format
Only a JSON body is accepted — there is noapplication/x-www-form-urlencoded
support.
Success response (default)
Failure response (default)
LoginConfig fields
There is no built-in login rate limiter — front the handler with
ratelimit.LoginThrottle yourself (this is what
securityhttp.WithLoginThrottle wires up).
Logout handler
LogoutConfig.Store is required (panics if nil). Logout is POST-only (a GET
gets 405) and idempotent — an absent or invalid session still clears cookies
and returns success.
What happens
- Session is invalidated (
store.Delete(sessionID)), best-effort - Session cookie is cleared
- Remember-me cookie is cleared, if
LogoutConfig.RememberMeis set - Response: 200 JSON
{"loggedOut": true}(note the field isloggedOut, notlogout) — override withLogoutConfig.OnLogout
web/csrf.Middleware (or securityhttp.New/NewHandler, which enables CSRF by
default once a session store is configured) requests need a valid
X-CSRF-Token — see csrf.
MFA handlers
Two-step login:MFALoginHandler runs the primary credential and begins the
challenge; MFAStepHandler completes it. Both take an authn.MFAManager (see
authn for MFAManagerConfig, MFAFactor, and MFAPolicy).
MFALoginHandler panics if Primary, MFAManager, or Store is nil.
MFAStepHandler panics if MFAManager or Store is nil (it does not use
Primary).
Magic link handlers
Issue and consume single-use authentication links viaauthn.MagicLinkStore
(see authn):
emailSender is anything implementing authn.MagicLinkSender:
MagicLinkRequestHandler requires Store and Sender. MagicLinkVerifyHandler
requires Store, Manager, and Session.
Passkey handlers
WebAuthn registration and assertion (sign-in), backed by anauthn.PasskeyAuthenticator you implement (see authn):
GetUserHandle extracts the WebAuthn user handle for the current caller and
is required by both registration handlers:
core.AuthenticationFrom resolves — the handlers return 401 otherwise.
