-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
42 lines (34 loc) · 959 Bytes
/
proxy.ts
File metadata and controls
42 lines (34 loc) · 959 Bytes
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
37
38
39
40
41
42
import { NextResponse, type NextRequest } from "next/server";
import { SESSION_COOKIE_NAME } from "./src/lib/auth";
const PUBLIC_PATHS = [
"/login",
"/api/auth/login",
"/api/health",
"/manifest.webmanifest",
];
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
if (
pathname.startsWith("/_next") ||
pathname.startsWith("/icons") ||
pathname === "/favicon.ico"
) {
return NextResponse.next();
}
if (PUBLIC_PATHS.some((p) => pathname === p || pathname.startsWith(p))) {
return NextResponse.next();
}
const session = request.cookies.get(SESSION_COOKIE_NAME);
if (!session?.value) {
const loginUrl = request.nextUrl.clone();
loginUrl.pathname = "/login";
if (pathname !== "/") {
loginUrl.searchParams.set("from", pathname);
}
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ["/(.*)"],
};