| 21 | ].filter(Boolean) as string[]; |
| 22 | |
| 23 | export async function proxy(request: NextRequest) { |
| 24 | const url = new URL(request.url); |
| 25 | const path = url.pathname; |
| 26 | |
| 27 | if (path === "/" && request.cookies.has("next-auth.session-token")) { |
| 28 | return NextResponse.redirect(new URL("/dashboard/caps", url.origin)); |
| 29 | } |
| 30 | |
| 31 | if (path.startsWith("/login")) { |
| 32 | const response = NextResponse.next(); |
| 33 | response.headers.set("X-Frame-Options", "SAMEORIGIN"); |
| 34 | response.headers.set( |
| 35 | "Content-Security-Policy", |
| 36 | "frame-ancestors https://cap.so", |
| 37 | ); |
| 38 | return response; |
| 39 | } |
| 40 | |
| 41 | const hostname = url.hostname; |
| 42 | |
| 43 | if (buildEnv.NEXT_PUBLIC_IS_CAP !== "true") { |
| 44 | if ( |
| 45 | !( |
| 46 | path.startsWith("/s/") || |
| 47 | path.startsWith("/c/") || |
| 48 | path.startsWith("/middleware") || |
| 49 | path.startsWith("/dashboard") || |
| 50 | path.startsWith("/onboarding") || |
| 51 | path.startsWith("/api") || |
| 52 | path.startsWith("/login") || |
| 53 | path.startsWith("/signup") || |
| 54 | path.startsWith("/invite") || |
| 55 | path.startsWith("/self-hosting") || |
| 56 | path.startsWith("/download") || |
| 57 | path.startsWith("/terms") || |
| 58 | path.startsWith("/verify-otp") || |
| 59 | path.startsWith("/embed/") || |
| 60 | path.startsWith("/.well-known/workflow/") |
| 61 | ) && |
| 62 | process.env.NODE_ENV !== "development" |
| 63 | ) |
| 64 | return NextResponse.redirect(new URL("/login", url.origin)); |
| 65 | else return NextResponse.next(); |
| 66 | } |
| 67 | |
| 68 | if (mainOrigins.some((d) => url.origin.startsWith(d))) { |
| 69 | return NextResponse.next(); |
| 70 | } |
| 71 | |
| 72 | const webUrl = new URL(serverEnv().WEB_URL).hostname; |
| 73 | |
| 74 | try { |
| 75 | if (!(path.startsWith("/s/") || path.startsWith("/c/"))) { |
| 76 | const url = new URL(request.url); |
| 77 | url.hostname = webUrl; |
| 78 | return NextResponse.redirect(url); |
| 79 | } |
| 80 | |