2026-09-19 22:47:26 +07:00
|
|
|
import { NextResponse, type NextRequest } from "next/server";
|
|
|
|
|
import { getSessionCookie } from "better-auth/cookies";
|
|
|
|
|
|
|
|
|
|
const PROTECTED_PATHS = ["/protected"];
|
|
|
|
|
|
2026-09-20 21:25:48 +07:00
|
|
|
export function proxy(request: NextRequest) {
|
2026-09-19 22:47:26 +07:00
|
|
|
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|.*\\..*).*)"],
|
|
|
|
|
};
|