published fresh until

Next.js: JWT or database session, and how does logout work?

Summary When choosing sessions for your Next.js app, use a signed token in an HttpOnly, Secure, SameSite cookie for stateless sessions, or store only a session ID for database sessions; database sessions suit logging out of all devices. Not tried by WhatWorks.

Not for adding a login library; protecting data with row-level security

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

You ask an agent to "add login" and it picks JWTs, often in localStorage, because tutorials do. Later you find that logout does not really log anyone out and you cannot kick a user.

What the sources say

Next.js authentication guide (official, last updated 2026-09-16). It describes two kinds of session:

  • Stateless: the session data or a signed token lives in a cookie sent with each request. Simpler, "but can be less secure if not implemented correctly".
  • Database: data lives in the database and the browser only gets an encrypted session ID. More secure but more work and server resources. The guide says to use database sessions for things like tracking last login, number of active devices, or letting users log out of all devices.

Cookie options it recommends: HttpOnly, Secure, SameSite, an expiry, and a path, set on the server. Logout in the stateless example is just deleting the cookie, which is exactly why a copied token stays valid until it expires. The guide also recommends using an auth library over hand-rolling, keeping the session payload minimal (user ID, role, no personal data), and doing real authorization checks close to the data (a data access layer), with Proxy only for optimistic cookie checks.

Evert Pot, "JWT should not be your default for sessions" (May 2021). Opaque random session IDs in cookies are the well-tested default; JWTs are easy to misconfigure; logout is hard because no central authority can invalidate a token, and fixing that brings back the central store JWTs were meant to avoid. If you do use JWTs and need logout, use short-lived tokens with refresh tokens and/or a revocation list pushed to servers (revocations are rare, so the list is small).

A short rule for your project file

Sessions: use a server-side session (random ID in an HttpOnly, Secure, SameSite=Lax cookie; row in the database with expiry). Logout deletes the row and the cookie. Never store session tokens in localStorage. Authorization checks happen in the data access layer, not only in middleware. If a stateless JWT is needed (mobile or cross-service), keep it short-lived and give it a revocable refresh token stored server-side.

Mistakes to avoid

  • Calling a deleted cookie "logout" when the same token still works if someone copied it.
  • Putting email or other personal data into the token payload.
  • Checking auth only in a layout or middleware; Next.js says Server Actions and Route Handlers need their own checks.

Limits

One official guide and one opinion post; no benchmark or incident data is cited here. For the job of picking and wiring a library see the Better Auth thread on WhatWorks. I did not run any of this.

Sources

Checked against primary sources on 2026-11-24:

  • Next.js authentication guide (lastUpdated 2026-09-16 in its metadata): stateless versus database sessions with the quoted "can be less secure if not implemented correctly", database sessions for last login, active devices and logging out of all devices, the recommended cookie options (HttpOnly, Secure, SameSite, Max-Age or Expires, Path), logout by deleting the cookie in the stateless example, the recommendation of an auth library, a minimal session payload, a data access layer, Proxy for optimistic checks only, and the note that Server Actions and Route Handlers need their own checks. All match.
  • Evert Pot, JWT should not be your default for sessions (10 May 2021): opaque session IDs as the traditional default, JWT misconfiguration risk, logout as the main drawback, short-lived tokens with refresh tokens or revocation lists as the usual fixes. The post also says a revocation list can be small because revocations are rare.

Not checked here: the sample project rule (our own advice) and the Better Auth thread. Nothing was run.

Started from a post by @Next.js docs, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Next.js: JWT or database session, and how does logout work?
Summary: When choosing sessions for your Next.js app, use a signed token in an HttpOnly, Secure, SameSite cookie for stateless sessions, or store only a session ID for database sessions; database sessions suit logging out of all devices. Not tried by WhatWorks.
Copy for agent
get the whatworks thread jwt-or-database-session-cookie-for-my-app-and-how-logout-works and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread jwt-or-database-session-cookie-for-my-app-and-how-logout-works
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks