-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.ts
More file actions
51 lines (40 loc) · 1.53 KB
/
Copy pathproxy.ts
File metadata and controls
51 lines (40 loc) · 1.53 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
44
45
46
47
48
49
50
51
import { getSessionCookie } from "better-auth/cookies"
import { NextResponse, type NextRequest } from "next/server"
const protectedPrefixes = ["/dashboard", "/runs", "/targets", "/settings", "/scans"] as const
function canUseDevelopmentActor() {
return process.env.NODE_ENV !== "production" && process.env.STACKRAY_ENABLE_DEV_ACTOR === "true"
}
function canUseDemoActor() {
return process.env.STACKRAY_ENABLE_DEMO === "true"
}
function matchesPrefix(pathname: string, prefix: string) {
return pathname === prefix || pathname.startsWith(`${prefix}/`)
}
export function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname
const hasSessionCookie = Boolean(getSessionCookie(request))
const hasSessionAccess = hasSessionCookie || canUseDevelopmentActor() || canUseDemoActor()
const requestHeaders = new Headers(request.headers)
requestHeaders.set("x-stackray-pathname", pathname)
if (pathname === "/change-password") {
if (!hasSessionAccess) {
return NextResponse.redirect(new URL("/", request.url))
}
return NextResponse.next({
request: {
headers: requestHeaders,
},
})
}
if (protectedPrefixes.some((prefix) => matchesPrefix(pathname, prefix)) && !hasSessionAccess) {
return NextResponse.redirect(new URL("/", request.url))
}
return NextResponse.next({
request: {
headers: requestHeaders,
},
})
}
export const config = {
matcher: ["/dashboard/:path*", "/runs/:path*", "/targets/:path*", "/settings/:path*", "/scans/:path*", "/change-password"],
}