AnonMessage uses a custom Next.js middleware file atDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/dev0302/nextjs-project-1/llms.txt
Use this file to discover all available pages before exploring further.
src/middleware.ts to enforce authentication rules before any request ever reaches a page or API route. It implements bidirectional redirect logic: unauthenticated users are pushed to sign-in when they try to reach the dashboard, and already-authenticated users are pushed to the dashboard when they try to reach sign-in or sign-up.
Full Middleware Source
What Middleware Does
Next.js middleware runs on the Edge Runtime before the matched request is forwarded to any server component, client component, or API route handler. It has access to the incomingNextRequest object and can inspect headers, cookies, and the URL, then return a NextResponse that either allows the request through (NextResponse.next()), redirects it, or rewrites it.
In AnonMessage, middleware is used for one purpose: reading the NextAuth JWT session cookie and making routing decisions based on whether a valid token exists.
Route Protection Logic
Extract the JWT token
getToken (from next-auth/jwt) reads the session cookie from the incoming request, decrypts it using NEXTAUTH_SECRET, and returns the decoded JWT payload as a plain object. If no valid session cookie exists — or if it cannot be decrypted — it returns null.Redirect authenticated users away from auth pages
token is not null) and tries to navigate to /sign-in or /sign-up, they are immediately redirected to /dashboard. This prevents already-logged-in users from seeing the sign-in form.Redirect unauthenticated users away from protected pages
/dashboard, the user is redirected to /sign-in. The startsWith('/dashboard') check covers all nested dashboard routes (e.g., future sub-pages like /dashboard/settings).The matcher Config
matcher array tells Next.js which URL paths should trigger the middleware function. Requests to paths not listed in matcher bypass middleware entirely — the function is never called for them.
The matcher
'/dashboard/:path*' uses Next.js route pattern syntax. The :path* segment matches zero or more path segments, so it covers /dashboard, /dashboard/settings, /dashboard/anything/nested, and so on. The plain string entries /sign-in and /signup match those exact paths only.Why Only These Routes?
Limiting middleware execution to a small set of paths is a performance best practice. Middleware runs on every matched request, so including paths like/api/* or static assets would add unnecessary latency. By restricting to the three auth-sensitive routes, middleware overhead is essentially invisible.
Token Extraction and NEXTAUTH_SECRET
getToken({ req: request }) and the NextAuth session system both read the same NEXTAUTH_SECRET environment variable to sign and verify the JWT. This variable must be identical in both places.
NEXTAUTH_SECRET differs between the two contexts (for example, one is set and the other is not), getToken will fail to decrypt the cookie and return null, causing all authenticated users to be treated as guests.
Why Not Use NextAuth’s Default Middleware?
NextAuth ships with a ready-to-use middleware export:| Scenario | Default NextAuth middleware | AnonMessage custom middleware |
|---|---|---|
Guest visits /dashboard | ✅ Redirects to sign-in | ✅ Redirects to sign-in |
Logged-in user visits /sign-in | ❌ No redirect — shows sign-in form | ✅ Redirects to /dashboard |
Logged-in user visits /sign-up | ❌ No redirect — shows sign-up form | ✅ Redirects to /dashboard |
Edge Runtime Constraints
Next.js middleware always runs in the Edge Runtime — a lightweight V8-based environment that does not include the full Node.js standard library. This means middleware must not import Node.js-only modules.getToken from next-auth/jwt is safe to use in middleware because it only performs cryptographic JWT verification using Web Crypto APIs, which are available in the Edge Runtime.