Secure, deploy, and maintain an application

Protect App Router Paths with a Next.js 16 Proxy

Add a Next.js 16 proxy.ts cookie gate for protected App Router paths, set its matcher, and understand what the redirect does not secure.

Editorial illustration for Protect App Router Routes with Next.js 16 Proxy

If a visitor requests /dashboard without a session cookie, you can send them to /login before the request continues. In Next.js 16, put that request-level check in proxy.ts and use config.matcher to say which paths it covers. Treat the result as an early redirect, not proof that anyone is signed in.

Add the request-level gate

Create proxy.ts at the project root, alongside app. If your project keeps app inside src, put proxy.ts in src instead. The Next.js integration guidance from Clerk documents those locations. For the Next.js 16 naming convention, Better Auth’s Next.js integration guide describes the move from middleware.ts and a middleware export to proxy.ts and a proxy export.

import { NextRequest, NextResponse } from 'next/server'

export function proxy(request: NextRequest) {
  // Optimistic gate only: cookie presence is not session validation.
  if (!request.cookies.has('session')) {
    return NextResponse.redirect(new URL('/login', request.url))
  }

  return NextResponse.next()
}

export const config = {
  matcher: ['/dashboard/:path*', '/admin/:path*'],
}

Replace session with the cookie name your authentication system actually uses. If its name or prefix is configurable, this check must agree with that configuration. request.cookies.has(...) asks only whether the incoming request contains a cookie of that name; it does not inspect its value or consult your session store. The missing-cookie branch redirects to /login. The other branch lets the request continue through this Proxy check.

Match the paths you mean to gate

Here, /dashboard/:path* selects /dashboard and paths beneath it, such as /dashboard/settings. The /admin/:path* entry does the same for /admin. The * modifier means zero or more path segments; this is why the base path is included rather than only its children. Next.js matcher documentation describes that path syntax and the incoming-request cookie methods. That documentation uses the older Middleware name, so keep the Next.js 16 proxy.ts naming from the example above.

Neither /login nor an unrelated public path such as /about is in this matcher. That matters for /login: including it would apply the missing-cookie redirect to the destination of the redirect itself. If your protected area grows, update the matcher deliberately; placing a page under app does not add its URL to these two patterns.

Check the redirect—and the boundary

You can check the intended behavior by requesting each path first without the configured cookie, then with it:

Request Expected result from this Proxy
/dashboard, no session cookie Redirect to /login
/dashboard/settings, no session cookie Redirect to /login
/admin, no session cookie Redirect to /login
/login or /about, no cookie No redirect from this Proxy; neither path matches
/dashboard, session cookie present Continue past this presence check

That last row is intentionally narrow. Someone can supply a cookie that exists but does not represent a valid session. Better Auth explicitly warns that a cookie-existence check can be bypassed by manually creating a cookie and must not be the sole protection for pages or actions.

Keep the decisive check in server-side code that reads or changes protected data: validate the session there, then check whether that user has permission for the resource or operation. This applies even when the Proxy has allowed the request to continue. Next.js authentication guidance places the main security checks in the data-access layer, with checks for server-side actions and route handlers as well. For the implementation of that boundary, see Next.js Authentication: Verify the Session Before Authorizing Data. The Proxy’s job in this example is smaller and useful: spare visitors without even the expected cookie a trip through a protected path.

Find a note

Search by topic, title, or keyword.