-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
36 lines (27 loc) · 1.01 KB
/
Copy pathproxy.ts
File metadata and controls
36 lines (27 loc) · 1.01 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
import { NextRequest, NextResponse } from "next/server"
import { requestHasAdminSession } from "@/src/lib/admin-session"
function unauthorizedResponse(request: NextRequest) {
if (request.nextUrl.pathname.startsWith("/api/admin")) {
return NextResponse.json({ success: false, error: "Unauthorized" }, { status: 401 })
}
const loginUrl = new URL("/sign-in", request.url)
loginUrl.searchParams.set("next", request.nextUrl.pathname)
return NextResponse.redirect(loginUrl)
}
export async function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname
if (pathname === "/api/admin/session" && request.method === "POST") {
return NextResponse.next()
}
if (pathname === "/admin/login") {
return NextResponse.redirect(new URL("/sign-in", request.url))
}
const hasSession = await requestHasAdminSession(request)
if (!hasSession) {
return unauthorizedResponse(request)
}
return NextResponse.next()
}
export const config = {
matcher: ["/admin/:path*", "/api/admin/:path*"],
}