Secure, stateless, and cookie-based session library for Next.js or any JavaScript framework
Secure, stateless, and cookie-based session library for Next.js or any JavaScript framework
iron-session is a secure, stateless, and cookie-based session library for JavaScript.
[!IMPORTANT] Coming from v8? Read Upgrading to v9 for the two changes most apps need, or MIGRATION.md for the full guide. v9 needs Node 22.13+ and is ESM-only.
The session data is stored in signed and encrypted cookies which are decoded by your server code in a stateless fashion (= no network involved). This is the same technique used by frameworks like Ruby On Rails.
Online demo and examples: https://get-iron-session.vercel.app
Featured in the Next.js documentation ⭐️
getIronSession<T>(req, res, sessionOptions): Promise<IronSession<T>>getIronSession<T>(cookieStore, sessionOptions): Promise<IronSession<T>>nodeCookies, webCookies, nextProxyCookiessession.save(): Promise<void>session.destroy(): voidsession.updateConfig(sessionOptions: SessionOptions): voidsealData(data: unknown, { password, ttl }): Promise<string>unsealData<T>(seal: string, { password, ttl }): Promise<T>pnpm add iron-sessionv9 needs Node 22.13 or later and is ESM-only. require() still works on
Node 22.13+, which supports require() of an ES module. If you are stuck on an
older Node, stay on v8: pnpm add iron-session@8.
Most apps change two things. Both are things v8 got wrong quietly.
1. Store timestamps, not Date objects.
- session.lastSeen = new Date();
+ session.lastSeen = Date.now();v8 turned a Date into a string when sealing, so the type you wrote was not the
type you read back. v9 throws and names the field.
2. Handle a session that does not exist yet.
- const userId = session.user.id;
+ const userId = session.user?.id;Reads are typed as Partial<T> now. A first visit, an expired cookie and a
destroy() all leave you an empty object, so the old type let this compile and
then throw at runtime.
Nothing else is required. getIronSession(req, res, options) and
getIronSession(await cookies(), options) both still work, v9 reads v8 cookies
and v8 reads v9 cookies, so you can roll a deploy back without signing everyone
out. If you had as any on await cookies(), delete it.
Worth adopting while you are here:
nextProxyCookies if you ever tried to save a session in Next.js
middleware and it did not stick.onUnsealError to see why cookies get
rejected instead of guessing.chunk: true if your session outgrew one cookie.The full guide, including the removed APIs and the security fix that signs pre-v8 cookies out once, is in MIGRATION.md.
We have extensive examples here too: https://get-iron-session.vercel.app/.
To get a session, there's a single method to know: getIronSession.
// Next.js API Routes and Node.js/Express/Connect.
import { getIronSession } from "iron-session";
export async function get(req, res) {
const session = await getIronSession(req, res, { password: "...", cookieName: "..." });
return session;
}
export async function post(req, res) {
const session = await getIronSession(req, res, { password: "...", cookieName: "..." });
session.username = "Alison";
await session.save();
}// Next.js Route Handlers (App Router)
import { cookies } from "next/headers";
import { getIronSession } from "iron-session";
export async function GET() {
const session = await getIronSession(await cookies(), { password: "...", cookieName: "..." });
return session;
}
export async function POST() {
const session = await getIronSession(await cookies(), { password: "...", cookieName: "..." });
session.username = "Alison";
await session.save();
}// Next.js Server Components and Server Actions (App Router)
import { cookies } from "next/headers";
import { getIronSession } from "iron-session";
async function getIronSessionData() {
const session = await getIronSession(await cookies(), { password: "...", cookieName: "..." });
return session;
}
async function Profile() {
const session = await getIronSessionData();
return
;
}// Next.js proxy.ts (middleware.ts before Next 16)
import { NextResponse, type NextRequest } from "next/server";
import { getIronSession, nextProxyCookies } from "iron-session";
export async function proxy(request: NextRequest) {
const response = NextResponse.next();
const session = await getIronSession(nextProxyCookies(request, response), options);
session.lastSeen = Date.now();
await session.save();
return response;
}Middleware needs the adapter because Next only merges a cookie into the current
render when it goes through response.cookies.set(). Writing a raw Set-Cookie
header there looks like it works and then has no effect.
Runnable examples for every pattern: https://get-iron-session.vercel.app/. Two of them are where to start, and they follow the Next.js authentication guide:
cacheComponents on. Also covers useActionState for form errors and
session rotation in proxy.ts.Three rules once cacheComponents is on:
<Suspense> boundary and the rest of the page still prerenders.use cache. Runtime APIs are rejected there, and
whatever it renders is shared between visitors.export const dynamic = "force-dynamic" is no longer allowed, and no longer
needed. The <Suspense> boundary is what marks the dynamic part.The session belongs next to the data it protects: read it in the Server
Component, Server Action or Route Handler that needs it. A layout does not
protect the pages under it, and neither does a redirect in proxy.ts.
getIronSession(req, res, options) covers Node, Express, Connect and Next.js
API routes, and getIronSession(await cookies(), options) covers the Next.js App
Router. When you want to be explicit, or when your framework hands you something
else, pass an adapter instead:
| Adapter | For |
|---|---|
nodeCookies(req, res) |
Node http, Express, Connect, Next.js API routes |
webCookies(request, responseOrHeaders) |
Anything web-standard: Hono, Bun, Deno, Cloudflare Workers, Route Handlers |
nextProxyCookies(request, response) |
Next.js Proxy (middleware), proxy.ts |
Anything with get(name) and set(name, value, options), like Next's
cookies(), can be passed directly. If your framework has neither, a cookie jar
is two functions:
const session = await getIronSession(
{
read: (name) => myFramework.getCookie(name),
write: (name, value, options) => myFramework.setCookie(name, value, options),
},
options,
);A browser refuses a cookie over 4096 bytes, and iron-session throws rather than letting one be silently dropped. Encryption adds overhead, so plan for roughly 3KB of actual data.
If you need more, chunk: true splits the session across several cookies. Before
you reach for it, know what the real limit is: every cookie is sent on every
request, and proxies cap the whole Cookie header well below what a few
chunks produce. nginx allows 8KB by default and a CDN in front of it may allow
less. Going over returns a 400 or 431 at the edge, before your code runs.
iron-session refuses more than 4 chunks for that reason.
The scalable answer is to keep an id in the session and the data in your database:
session.userId = user.id; // small, stateless
const user = await db.user.findUnique({ where: { id: session.userId } });When a cookie cannot be read, iron-session starts a new empty session instead of throwing. It has to: it cannot tell a tampered cookie from a password you rotated out or a seal that simply expired, and a 500 on every request would be worse. That makes real problems invisible, so log them:
const options = {
cookieName: "session",
password: process.env.SESSION_PASSWORD,
onUnsealError: (reason, error) => {
// "expired" is normal, that is how sessions end.
if (reason !== "expired") {
logger.warn({ reason, error }, "session cookie rejected");
}
},
};A burst of "unknown-password" usually means a password rotation went wrong. A
burst of "invalid" can mean someone is probing your cookies.
There is no validate option, on purpose. If you change the shape of your
session, old cookies still decrypt into the old shape, and the place to handle
that is the wrapper you already have:
// lib/session.ts
export async function getSession() {
const session = await getIronSession<Session>(await cookies(), options);
if (session.user && !SessionSchema.safeParse({ ...session }).success) {
session.destroy();
}
return session;
}✅ Production ready and maintained.
Two options are required: password and cookieName. Everything else is automatically computed and usually doesn't need to be changed.
password, required: Private key used to encrypt the cookie. It has to be at least 32 characters long. Use https://1password.com/password-generator/ to generate strong passwords. password can be either a string or an object with incrementing keys like this: {2: "...", 1: "..."} to allow for password rotation. iron-session will use the highest numberedNo open issues yet, or sync has not completed.