-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
30 lines (24 loc) · 837 Bytes
/
Copy pathproxy.ts
File metadata and controls
30 lines (24 loc) · 837 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
if (pathname === "/admin/login") {
return NextResponse.next();
}
if (pathname.startsWith("/admin")) {
const session = request.cookies.get("gf_admin")?.value;
const password = process.env.ADMIN_PASSWORD || "";
const expected = Array.from(new TextEncoder().encode(password))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
if (!session || session !== expected) {
const login = new URL("/admin/login", request.url);
login.searchParams.set("from", pathname);
return NextResponse.redirect(login);
}
}
return NextResponse.next();
}
export const config = {
matcher: "/admin/:path*",
};