Add Session Management

Select a session management client to store your authentication tokens and user info.

Now you'll need to choose a session management client to store authentication tokens and user data for logged-in users. For more details on how session management works, view our Session Management documentation .

๐Ÿ’ก

Do you need session management?

Short answer: Yes.

While session management is technically optional, it's very strongly recommended for most applications. Only skip session management if your existing architecture makes implementation impractical.


Session Framework Options

This guide utilizes Iron Session for session management, but here's a comparison of popular Node.js session management options for reference:

FeatureIron Sessionexpress-sessioncookie-session
Storage TypeEncrypted cookieServer-side (memory, DB, Redis)Encrypted cookie
Stateful/StatelessStatelessStatefulStateless
Encryptionโœ… Yes (built-in)โŒ No (relies on secure cookies)โœ… Yes
Ease of Useโญโญโญโญโญ (Very simple, minimal config)โญโญโญ (Requires session store infra setup)โญโญโญโญ (Simple for small apps, lacks encryption)
Authentication Included?โŒ NoโŒ NoโŒ No
Ideal ForStateless, secure sessions in Next.js & Node.jsTraditional apps needing persistent sessionsLightweight apps needing simple session storage
Setup Complexity๐Ÿ”น Simple (just configure cookies)โš ๏ธ Medium (needs a store)๐Ÿ”น Simple (config-only)
Performance๐Ÿš€ Fast (stateless)๐Ÿข Slower (stateful)๐Ÿš€ Fast (stateless)
Key TakeawaysBest for stateless, encrypted cookie-based sessions with minimal setup.Better for server-side session storage with databases.Lightweight alternative but lacks the advanced encryption of Iron Session.



Install Session Library

For this guide, use your package manager CLI to install Iron Session into your project.

npm install iron-session
yarn add iron-session
pnpm add iron-session

Configure Session Middleware

Configure Iron Session middleware to enable encrypted, cookie-based sessions (e.g. src/app.ts):

// app.ts
import { getIronSession } from 'iron-session';

...

// Initialize Iron Session middleware for cookie-based sessions.
app.use(async (req, res, next) => {
  req.session = await getIronSession(req, res, {
    cookieName: 'my-session-cookie-name',
    password: 'my-session-cookie-password', // 32-character minimum
    cookieOptions: {
      httpOnly: true,
      maxAge: 1800, // e.g. 30 minutes; Ideally, it should match your access token expiration.
      path: '/',
      sameSite: true, // May need to set this to 'Lax' if dealing with CORS in your environment
      secure: false, // IMPORTANT: set to true for production environments!!
    },
  });
  next();
});

...

Now the session for each authenticated user will be accessible on the req.session field throughout your Express routes and controllers.

Next, let's enhance the auth routes to add session management logic. We'll also introduce a new Session endpoint for loading session data into your frontend.