auth/session_cookie.ts view source
SessionOptions<TIdentity> Configuration for a session cookie format.
Apps provide encode/decode to control the identity portion
of the cookie payload.
The TIdentity type parameter determines the trust model:
- string (e.g. a session_id) — the cookie references a server-side session record,
enabling per-session revocation and metadata. Use when you need admin controls
like "revoke all sessions" or per-session audit trails.
- number (e.g. an account_id) — the cookie directly encodes the user identity,
requiring no server-side session state. Simpler, but individual sessions
can only be invalidated by rotating the signing key (which invalidates all sessions).
examples
const tx_config: SessionOptions<string> = {
cookie_name: 'tx_session',
context_key: 'auth_session_id',
encode_identity: (session_id) => `admin:${session_id}`,
decode_identity: (payload) => {
const parts = payload.split(':');
if (parts.length !== 2 || parts[0] !== 'admin') return null;
return parts[1] || null;
},
};
const visiones_config: SessionOptions<number> = {
cookie_name: 'session_id',
context_key: 'auth_session_id',
encode_identity: (id) => String(id),
decode_identity: (payload) => {
const n = parseInt(payload, 10);
return Number.isFinite(n) && n > 0 ? n : null;
},
};
context_key
Hono context variable name for the identity.
type string
cookie_options
type Partial<SessionCookieOptions>
encode_identity
Encode identity into the cookie payload (before the :expires_at suffix).
type (identity: TIdentity) => string
decode_identity
Decode identity from cookie payload. Return null if invalid.
type (payload: string) => TIdentity | null