-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
36 lines (30 loc) · 897 Bytes
/
middleware.ts
File metadata and controls
36 lines (30 loc) · 897 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
import { NextRequest, NextResponse } from "next/server"
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// Protect admin routes
if (pathname.startsWith("/admin")) {
const adminSecret = process.env.ADMIN_SECRET
if (!adminSecret) {
return NextResponse.json(
{ error: "Admin authentication not configured" },
{ status: 500 }
)
}
// Check for admin secret in query params
const secretParam = request.nextUrl.searchParams.get("admin_secret")
if (secretParam !== adminSecret) {
return NextResponse.json(
{
error: "Unauthorized",
message:
"Admin authentication required. Add ?admin_secret=YOUR_SECRET to the URL",
},
{ status: 401 }
)
}
}
return NextResponse.next()
}
export const config = {
matcher: ["/admin/:path*"],
}