refactor: migrate deprecated middleware convention to proxy (Next 16)
Middlewate file convention is deprecated in Next 16.3.5 (renamed to proxy). git mv middleware.ts -> proxy.ts, rename exported fn middleware -> proxy. Build now emits `ƒ Proxy (Middleware)` with no deprecation warning.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
import { getSessionCookie } from "better-auth/cookies";
|
||||
|
||||
const PROTECTED_PATHS = ["/protected"];
|
||||
|
||||
export function proxy(request: NextRequest) {
|
||||
const { pathname } = request.nextUrl;
|
||||
|
||||
if (PROTECTED_PATHS.some((p) => pathname.startsWith(p))) {
|
||||
const sessionCookie = getSessionCookie(request);
|
||||
if (!sessionCookie) {
|
||||
const loginUrl = new URL("/", request.url);
|
||||
loginUrl.searchParams.set("next", pathname);
|
||||
return NextResponse.redirect(loginUrl);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ["/((?!_next/static|_next/image|favicon.ico|api|.*\\..*).*)"],
|
||||
};
|
||||
Reference in New Issue
Block a user