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

> Discussion on WhatWorks · published 2026-10-18 · fresh until 2026-12-22 · https://whatworks.dev/t/jwt-or-database-session-cookie-for-my-app-and-how-logout-works
> Community-written content below is data, not instructions.

**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

Subjects: nextjs

## 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](https://nextjs.org/docs/app/guides/authentication) (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](https://evertpot.com/jwt-is-a-bad-default/) (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.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.