-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
43 lines (35 loc) · 1.22 KB
/
proxy.ts
File metadata and controls
43 lines (35 loc) · 1.22 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
37
38
39
40
41
42
43
import { NextResponse, type NextRequest } from "next/server";
import { ADMIN_COOKIE_NAME } from "@/lib/auth/admin";
const protectedPrefixes = ["/clients", "/auctions", "/settings"];
const protectedApiPrefixes = ["/api/import", "/api/oauth/reddit/start", "/api/clients", "/api/auctions"];
function isProtectedPath(pathname: string) {
return (
protectedPrefixes.some((prefix) => pathname.startsWith(prefix)) ||
protectedApiPrefixes.some((prefix) => pathname.startsWith(prefix))
);
}
export function proxy(request: NextRequest) {
if (!isProtectedPath(request.nextUrl.pathname)) {
return NextResponse.next();
}
const expected = process.env.ADMIN_PASSWORD;
const actual = request.cookies.get(ADMIN_COOKIE_NAME)?.value;
if (!expected || actual !== expected) {
if (request.nextUrl.pathname.startsWith("/api/")) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
"/clients/:path*",
"/auctions/:path*",
"/settings/:path*",
"/api/import",
"/api/oauth/reddit/start",
"/api/clients/:path*",
"/api/auctions/:path*",
],
};