Part 15 / Topic 2 of 5
The session itself: sealed, encrypted, stateless
// harness/app/account/lib/session-config.ts (excerpt)
export const sessionOptions: SessionOptions = {
cookieName: "quickcart_session",
password: process.env.SESSION_PASSWORD!, // openssl rand -base64 32, in .env.local (gitignored)
cookieOptions: { httpOnly: true, secure: process.env.NODE_ENV === "production", sameSite: "lax", maxAge: 60 * 60 * 24 * 7, path: "/" },
};
// harness/app/account/lib/session.ts
export async function getSession() {
return getIronSession<SessionData>(await cookies(), sessionOptions);
}
getIronSession(await cookies(), sessionOptions) is iron-session's own documented App Router shape — await cookies() already satisfies the CookieStore interface the library wants, no adapter needed on this side. The login Server Action uses it to create a session:
// harness/app/account/actions.ts (excerpt)
const session = await getSession();
session.userId = user.id;
await session.save();
redirect("/account/dashboard");
Run for real against this harness (the real hidden $ACTION_* fields, read from the login page exactly like Part 12 section 2, omitted here for brevity):
$ curl -s -c cookies.txt -X POST http://localhost:PORT/account/login -F ... -F '[email protected]' -F 'password=adminpass'
HTTP/1.1 303 See Other
Set-Cookie: quickcart_session=Fe26.2*1*786f3c2c8f305818fa3940b3894a66d1c69051bc72524aa6bb433551646655ee*...; Path=/; Expires=Tue, 08 Dec 2026 12:27:01 GMT; Max-Age=604800; Secure; HttpOnly; SameSite=lax
Location: /account/dashboard
Fe26.2*... is iron-session's own seal format (its underlying @hapi/iron encryption, versioned) — an opaque, encrypted, tamper-evident blob, not a readable JSON object the way an unsigned cookie would be. The Secure attribute is present because next start sets NODE_ENV=production; this harness is always exercised over http://localhost, which browsers and curl both treat as a secure context despite the plain http:// scheme, so the cookie still round-trips in every check below — a real non-HTTPS deployment would need to serve over TLS, or that same cookie would be silently dropped.
Logging in with the wrong password never creates a session at all — verifyCredentials (app/account/lib/users.ts) compares a scrypt hash with timingSafeEqual, and the Server Action returns the same generic error either way (no session created, no distinguishable timing, no hint about which field was wrong):
$ curl -s -X POST http://localhost:PORT/account/login -F ... -F '[email protected]' -F 'password=WRONG'
HTTP/1.1 200 OK
...<p role="alert">Invalid email or password.</p>...
Done here? The practice set for Part 15 — including the solutions — is on the part page.