Skip to content

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.

go
import "github.com/paulmanoni/nexus/v2/extension/session"

nexus.Boot(session.Module(session.Config{}))   // in-memory store, 14-day TTL
go
s := 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 cookie

The 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:

go
nexus.AsRest("POST", "/checkout/confirm", NewConfirm, session.Required())

Decorator form:

go
//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 ​

StoreSurvives
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.StoreWhatever your database gives you

The cookie is always HttpOnly, and SameSite defaults to Lax. Set Secure: true behind TLS.

Released under the MIT License.