Files
EpicNext-Cms/src/proxy.ts
T
Simo c4bf6488d0
Remote Build and Deploy / deploy (push) Successful in 43s
fix: use canonical Auth.js session in proxy
2026-07-11 22:55:26 +02:00

28 lines
1.1 KiB
TypeScript

import { NextResponse } from "next/server";
import { auth } from "@/lib/auth";
import { shouldRedirectAdminRequest } from "@/lib/proxy-access";
// Edge proxy (formerly "middleware"): Prisma can't run here, so we only forward
// the request path (so server components / the access guard can read it via
// headers()) and normalize the real client IP. The DB-backed banned/maintenance
// checks happen in src/lib/access-guard.ts (Node runtime) from the root layout.
export const proxy = auth((req) => {
if (shouldRedirectAdminRequest(req.nextUrl.pathname, req.auth?.user ?? null)) {
return NextResponse.redirect(new URL("/login", req.url));
}
const headers = new Headers(req.headers);
headers.set("x-pathname", req.nextUrl.pathname);
const ip =
req.headers.get("cf-connecting-ip") ??
req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ??
req.headers.get("x-real-ip") ??
"";
if (ip) headers.set("x-real-client-ip", ip);
return NextResponse.next({ request: { headers } });
});
export const config = {
matcher: ["/((?!api|_next/static|_next/image|assets|favicon.ico).*)"],
};