Next.js Protected Routes: Why a Session Cookie Check in proxy.ts Isn't Enough

2026-11-30来源 dev.toEnglish

I asked an AI assistant for a simple thing: a team dashboard in a Next.js 16 app (Better Auth + Drizzle + Postgres), visible only to signed-in users.

It looked done. Signed out, /dashboard sent me to the sign-in page. Signed in, I saw the team. If you only tested by clicking around, you would ship it.

Then I set one fake cookie and the page showed every name and email in the database.

All of this was tested on my own local app.

What the AI generated

Two files. First, proxy.ts (in Next.js 16 this replaced middleware.ts):

import { NextResponse, type NextRequest } from "next/server"; import { getSessionCookie } from "better-auth/cookies"; // Protect private pages: no session cookie, no access. export function proxy(request: NextRequest) { const sessionCookie = getSessionCookie(request); if (!sessionCookie) { return NextResponse.redirect(new URL("/sign-in", request.url)); } return NextResponse.next(); } export const config = { matcher: ["/dashboard/:path*", "/admin/:path*"], }; 

Second, the dashboard page:

// /dashboard is already protected by proxy.ts async function Members() { const members = await db.select().from(user); // ...renders every name and email } 

Read that comment again: already protected by proxy.ts. The page itself checks nothing.

The bug

getSessionCookie() only checks that a cookie with the right name exists. It does not ask Better Auth whether the cookie belongs to a real session. That is by design: the proxy runs on every matching request and is meant to stay fast, without a database call.

So I signed out. The session table was empty: no valid session existed at all. Then, in the browser console:

document.cookie = "better-auth.session_token=fake; path=/" 

Reload /dashboard: every user's name and email.

It is not a browser trick. Any script can do it:

curl -s -H "Cookie: better-auth.session_token=fake" localhost:3000/dashboard \ | grep -oE "[a-z.]+@acme\.dev" | sort -u 

(In production the cookie name gets a __Secure- prefix, but the idea is the same.)

The fix: check the session next to the data

The mistake is not the proxy. The mistake is trusting it. A small data access helper:

// src/lib/session.ts import { cache } from "react"; import { headers } from "next/headers"; import { redirect } from "next/navigation"; import { auth } from "./auth"; export const getSession = cache(async () => auth.api.getSession({ headers: await headers() }), ); export async function requireUser() { const session = await getSession(); if (!session) redirect("/sign-in"); return session; } 

And one line in the page, right before the query:

async function Members() { await requireUser(); const members = await db.select().from(user); // ... } 

auth.api.getSession() validates the session on the server against the database. cache() means it runs once per request, however many components call it.

The proxy stays exactly as it is. Now there are two layers:

  • proxy.ts: a fast, optimistic filter (no cookie, no entry).
  • requireUser(): the real check, next to the data.

Same fake cookie again: redirected to sign in. The curl command returns zero emails. A real signed-in user still sees the dashboard.

Why AI gets this wrong

For years the common pattern was "protect your routes in middleware and you're done". There is a lot of that code online. The Next.js docs now say the proxy is for quick optimistic checks, and real authorization belongs close to your data.

When you ask AI for auth, ask one more question: where exactly is the session validated? If the answer is "only in the proxy", you have this bug.

Three things to remember:

  1. The proxy only looks at the cookie.
  2. Validate the session next to the data, on the server.
  3. Test it yourself with a fake cookie. It takes ten seconds.

Video and code

I recorded the whole thing, build, break and fix, in 7 minutes:

Before/after code (tag before-fix vs main): https://github.com/10minutestack/ai-auth-fake-cookie

Have you found a bug like this in AI-generated auth code? I'd like to hear how you check for it.

原文:https://www.a8q9.cn/en/article/next-js-protected-routes-why-a-session-cookie-check-in-proxy-ts-isn-t-enough-af1688.html

相关文章