Repository navigation
Expand file tree
/
Copy pathproxy.ts
More file actions
134 lines (126 loc) · 4.19 KB
/
Copy pathproxy.ts
File metadata and controls
134 lines (126 loc) · 4.19 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { NextRequest, NextResponse } from "next/server";
import { loggingMiddleware } from "@/middleware/loggingMiddleware";
import { errorHandler } from "@/middleware/errorHandler";
import { auth } from "@/lib/auth/auth";
import { headers } from "next/headers";
import { env } from "@/env.mjs";
import { User } from "@/db/schema/02_user";
export async function proxy(request: NextRequest) {
const url = request.nextUrl.clone();
const redirectUrl = encodeURIComponent(request.nextUrl.pathname);
if (url.pathname.startsWith("/dashboard")) {
const session = await auth.api.getSession({
headers: await headers(),
});
if (!session) {
return NextResponse.redirect(
new URL(`/login?redirect=${redirectUrl}`, request.url),
);
}
const user = session.user as User;
if (user.banned) {
await auth.api.signOut({ headers: await headers() });
return NextResponse.redirect(new URL("/login?error=banned", request.url));
}
if (user.role === "pending") {
await auth.api.signOut({ headers: await headers() });
return NextResponse.redirect(
new URL(`/login?error=pending?redirect=${redirectUrl}`, request.url),
);
}
if (url.pathname === "/dashboard") {
return NextResponse.redirect(new URL(`/dashboard/home`, request.url));
}
return NextResponse.next();
}
if (url.pathname.startsWith("/api/auth")) {
return NextResponse.next();
}
if (url.pathname.startsWith("/api")) {
if (url.pathname.startsWith("/api/v1")) {
const apiEnabled = env.API_ENABLED;
if (!apiEnabled) {
return new NextResponse(
JSON.stringify({
message: "This API route does not exist.",
status: 404,
}),
{ status: 404, headers: { "Content-Type": "application/json" } },
);
}
const openapiEnabled = env.OPENAPI_ENABLED;
if (
!openapiEnabled &&
(url.pathname.startsWith("/api/v1/docs") ||
url.pathname.startsWith("/api/v1/openapi"))
) {
return new NextResponse(
JSON.stringify({
message: "This API route does not exist.",
status: 404,
}),
{ status: 404, headers: { "Content-Type": "application/json" } },
);
}
}
const routeExists = checkRouteExists(url.pathname);
if (!routeExists) {
return new NextResponse(
JSON.stringify({
message: "This API route does not exist.",
status: 404,
}),
{
status: 404,
headers: { "Content-Type": "application/json" },
},
);
}
}
try {
loggingMiddleware(request);
} catch (err) {
errorHandler(err);
}
}
function checkRouteExists(pathname: string) {
const routePatterns = [
/^\/api\/agent\/[^/]+\/status\/?$/,
/^\/api\/agent\/[^/]+\/backup\/?$/,
/^\/api\/agent\/[^/]+\/backup\/upload\/init\/?$/,
/^\/api\/agent\/[^/]+\/backup\/upload\/status\/?$/,
/^\/api\/agent\/[^/]+\/restore\/?$/,
/^\/api\/files\/images\/[^/]+\/?$/,
/^\/api\/files\/backups\/?$/,
/^\/api\/tus\/hooks\/?$/,
/^\/api\/events\/?$/,
/^\/api\/config\/?$/,
/^\/api\/health\/?$/,
/^\/api\/google\/drive\/callback\/?$/,
/^\/api\/avatar\/?$/,
// v1 external API
/^\/api\/v1\/mcp\/?$/,
/^\/api\/v1\/docs\/?$/,
/^\/api\/v1\/openapi\/?$/,
/^\/api\/v1\/agents\/?$/,
/^\/api\/v1\/agents\/[^/]+\/?$/,
/^\/api\/v1\/agents\/[^/]+\/key\/?$/,
/^\/api\/v1\/databases\/?$/,
/^\/api\/v1\/databases\/[^/]+\/?$/,
/^\/api\/v1\/databases\/[^/]+\/backup\/?$/,
/^\/api\/v1\/databases\/[^/]+\/backup\/[^/]+\/?$/,
/^\/api\/v1\/databases\/[^/]+\/restore\/?$/,
/^\/api\/v1\/databases\/[^/]+\/status\/?$/,
/^\/api\/v1\/databases\/[^/]+\/backup-policy\/?$/,
/^\/api\/v1\/organizations\/?$/,
/^\/api\/v1\/organizations\/[^/]+\/?$/,
/^\/api\/v1\/organizations\/[^/]+\/projects\/?$/,
/^\/api\/v1\/organizations\/[^/]+\/agents\/?$/,
/^\/api\/v1\/organizations\/[^/]+\/agents\/[^/]+\/?$/,
/^\/api\/v1\/projects\/[^/]+\/?$/,
];
return routePatterns.some((pattern) => pattern.test(pathname));
}
export const config = {
matcher: ["/api/:path*", "/dashboard/:path*", "/dashboard"],
};