Sessions
extension/session provides Django-style server-side sessions. A cookie carries an opaque ID, the data lives in a store, and handlers use a lazy handle. It works for anonymous visitors and signed-in users, on REST, Inertia and GraphQL.
import "github.com/paulmanoni/nexus/v2/extension/session"
nexus.Boot(session.Module(session.Config{})) // in-memory store, 14-day TTLs := session.Get(ctx)
s.Set("cart", skus) // the first write creates the ID and sets the cookie
s.Cycle() // rotate the ID on login (session fixation defense)
s.Destroy() // logout: delete the stored data and expire the cookieThe session is lazy:
- The store isn't read until the session is touched.
- Nothing is saved unless the session was modified.
s.Touch()forces a save. - The cookie is set on the first write, so write the session before the response body.
Values must round-trip through JSON.
Requiring a session
session.Required() is a per-op gate for flow continuity: it rejects requests that did not arrive with an established session — no cookie, or an unknown or expired id — with 428 Precondition Required, telling the client to restart the flow. Use it on the later steps of a multi-step form or checkout:
nexus.AsRest("POST", "/checkout/confirm", NewConfirm, session.Required())Decorator form:
//nexus:rest POST /checkout/confirm
//nexus:session Required
func NewConfirm(...) (...)It is not authentication — that stays auth.Required's job — and it fails closed when session.Module isn't installed. Handlers can ask the same question directly with s.Established(), which reports whether the request arrived with a live session (a session first written during the current request reports false).
Stores
| Store | Survives |
|---|---|
session.NewMemoryStore() (default) | nexus dev rebuilds, but not production restarts |
session.CacheStore(cache) | Restarts and multiple replicas, when the cache is Redis |
Your own session.Store | Whatever your database gives you |
The cookie is always HttpOnly, and SameSite defaults to Lax. Set Secure: true behind TLS.