-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
25 lines (21 loc) · 739 Bytes
/
Copy pathproxy.ts
File metadata and controls
25 lines (21 loc) · 739 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function proxy(request: NextRequest) {
if (request.nextUrl.pathname.startsWith("/admin")) {
const key
= request.nextUrl.searchParams.get("key")
?? request.cookies.get("admin-key")?.value;
if (!key || key !== process.env.ADMIN_KEY) {
return NextResponse.redirect(new URL("/", request.url));
}
// Set cookie so staff don't need ?key= on every page navigation
const response = NextResponse.next();
response.cookies.set("admin-key", key, {
httpOnly: true,
path: "/",
sameSite: "strict",
});
return response;
}
}
export const config = { matcher: ["/admin/:path*"] };