Authentication redirects
Protect dashboard or account routes by redirecting unauthenticated requests to login.
Next.js 16+ · Directive
Middleware runs before a request completes and can redirect, rewrite, or modify headers. In Next.js 16, the middleware convention is deprecated and replaced by Proxy, except when retaining Edge-runtime usage.
Middleware runs before a request is completed and can alter routing or headers based on the incoming request. In Next.js 16, the convention is deprecated in favor of Proxy; retain Middleware when the Edge runtime is required.
export function middleware(request: Request): Response | NextResponse| Name | Type | Description |
|---|---|---|
request | Request | The incoming request passed to the Middleware function. |
A Response or NextResponse instance, or no response when continuing the request.
export function middleware(request: Request) {
return Response.redirect(new URL('/login', request.url))
}
export const config = {
matcher: ['/dashboard/:path*'],
}Redirects dashboard requests to the login page before the request completes.
Protect dashboard or account routes by redirecting unauthenticated requests to login.
Send a request to a different route while keeping the visible URL unchanged.
Add or alter headers before a route handler or page processes the request.
ProxyNextResponsematcherEdge runtimeredirectrewrite