import { NextRequest, NextResponse } from "next/server"; const protectedPagePrefixes = [ "/feed", "/my-network", "/jobs", "/messages", "/notifications", "/profile", "/companies", "/search", "/premium", "/games", "/directory", ]; function isProtectedPage(pathname: string) { return protectedPagePrefixes.some((prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`)); } function isPublicCloneAuthMode() { return ["redis-resend", "local-dev"].includes(process.env.PUBLIC_CLONE_AUTH_MODE ?? ""); } const securityHeaders = { "x-content-type-options": "nosniff", "x-frame-options": "DENY", "referrer-policy": "strict-origin-when-cross-origin", "permissions-policy": "camera=(), microphone=(), geolocation=()", }; export function proxy(request: NextRequest) { const requestId = request.headers.get("x-request-id") ?? crypto.randomUUID(); const hasLocalPreviewSession = process.env.PUBLIC_CLONE_AUTH_MODE === "local-dev" && Boolean(request.cookies.get("linkedin_local_session")?.value); const hasSession = Boolean(request.cookies.get("linkedin_session")?.value) || hasLocalPreviewSession; const isApi = request.nextUrl.pathname.startsWith("/api"); if (!isApi && request.nextUrl.pathname === "/register" && isPublicCloneAuthMode()) { const publicRegisterUrl = new URL("/public-register", request.url); const redirect = NextResponse.redirect(publicRegisterUrl); redirect.headers.set("x-request-id", requestId); for (const [key, value] of Object.entries(securityHeaders)) redirect.headers.set(key, value); return redirect; } if (!isApi && isProtectedPage(request.nextUrl.pathname) && !hasSession) { const loginUrl = new URL("/login", request.url); loginUrl.searchParams.set("next", `${request.nextUrl.pathname}${request.nextUrl.search}`); const redirect = NextResponse.redirect(loginUrl); redirect.headers.set("x-request-id", requestId); for (const [key, value] of Object.entries(securityHeaders)) redirect.headers.set(key, value); return redirect; } const response = NextResponse.next({ request: { headers: request.headers, }, }); response.headers.set("x-request-id", requestId); for (const [key, value] of Object.entries(securityHeaders)) response.headers.set(key, value); if (request.nextUrl.pathname.startsWith("/api")) { response.headers.set("cache-control", "no-store"); } return response; } export const config = { matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"], };