-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathproxy.ts
More file actions
36 lines (29 loc) · 1.02 KB
/
proxy.ts
File metadata and controls
36 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Next.js 16 Proxy — lightweight cookie-existence check.
* Runs as a network boundary handler so unauthenticated visitors are
* redirected to /login instantly without starting a serverless function.
*
* NO fetch / API call / JWT verification here; server components and
* API routes handle full session validation.
*/
import { NextRequest, NextResponse } from "next/server";
const PUBLIC = new Set(["/login", "/register"]);
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
if (PUBLIC.has(pathname)) {
return NextResponse.next();
}
const session = request.cookies.get("session_id")?.value;
if (!session || session === "null" || session === "undefined") {
const url = request.nextUrl.clone();
url.pathname = "/login";
url.search = "";
return NextResponse.redirect(url);
}
return NextResponse.next();
}
export const config = {
matcher: [
"/((?!api|_next/static|_next/image|favicon\\.ico|.*\\.svg|.*\\.png|.*\\.jpg|.*\\.woff|.*\\.woff2).*)",
],
};