Files
asepharyana dcc6fc2c0a 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.
2026-09-20 21:27:28 +07:00

24 lines
679 B
TypeScript

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|.*\\..*).*)"],
};