2026-02-25 09:58:15 +01:00
|
|
|
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";
|
2026-05-28 20:44:27 +02:00
|
|
|
import { env } from "@/env.mjs";
|
|
|
|
|
import {User} from "@/db/schema/02_user";
|
|
|
|
|
|
2025-11-03 11:37:54 +01:00
|
|
|
export async function proxy(request: NextRequest) {
|
2026-02-25 09:58:15 +01:00
|
|
|
const url = request.nextUrl.clone();
|
|
|
|
|
const redirectUrl = encodeURIComponent(request.nextUrl.pathname);
|
2024-11-28 20:28:00 +01:00
|
|
|
|
2026-02-25 09:58:15 +01:00
|
|
|
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),
|
|
|
|
|
);
|
2024-11-28 20:28:00 +01:00
|
|
|
}
|
2026-05-28 20:44:27 +02:00
|
|
|
const user = session.user as User
|
|
|
|
|
|
|
|
|
|
if (user.banned) {
|
2026-02-25 09:58:15 +01:00
|
|
|
await auth.api.signOut({ headers: await headers() });
|
|
|
|
|
return NextResponse.redirect(new URL("/login?error=banned", request.url));
|
|
|
|
|
}
|
2026-05-28 20:44:27 +02:00
|
|
|
if (user.role === "pending") {
|
2026-02-25 09:58:15 +01:00
|
|
|
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();
|
|
|
|
|
}
|
2024-11-28 20:28:00 +01:00
|
|
|
|
2026-02-25 09:58:15 +01:00
|
|
|
if (url.pathname.startsWith("/api/auth")) {
|
|
|
|
|
return NextResponse.next();
|
|
|
|
|
}
|
2024-11-28 20:28:00 +01:00
|
|
|
|
2026-02-25 09:58:15 +01:00
|
|
|
if (url.pathname.startsWith("/api")) {
|
2026-05-28 22:08:49 +02:00
|
|
|
if (url.pathname.startsWith("/api/v1")) {
|
2026-06-02 18:36:35 +02:00
|
|
|
const apiEnabled = env.API_ENABLED;
|
2026-05-28 22:08:49 +02:00
|
|
|
if (!apiEnabled) {
|
|
|
|
|
return new NextResponse(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
message: "This API route does not exist.",
|
|
|
|
|
status: 404,
|
|
|
|
|
}),
|
|
|
|
|
{ status: 404, headers: { "Content-Type": "application/json" } },
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-06-02 18:36:35 +02:00
|
|
|
const openapiEnabled = env.OPENAPI_ENABLED;
|
2026-05-28 22:08:49 +02:00
|
|
|
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" } },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 09:58:15 +01:00
|
|
|
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" },
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-11-11 18:50:42 +01:00
|
|
|
}
|
2026-02-25 09:58:15 +01:00
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
loggingMiddleware(request);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
errorHandler(err);
|
|
|
|
|
}
|
2024-11-11 18:50:42 +01:00
|
|
|
}
|
2025-11-01 19:38:05 +01:00
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
function checkRouteExists(pathname: string) {
|
2026-02-25 09:58:15 +01:00
|
|
|
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\/?$/,
|
2026-05-17 09:46:03 +02:00
|
|
|
/^\/api\/health\/?$/,
|
2026-02-25 09:58:15 +01:00
|
|
|
/^\/api\/google\/drive\/callback\/?$/,
|
2026-05-25 15:36:33 +02:00
|
|
|
// v1 external API
|
2026-06-01 23:06:21 +02:00
|
|
|
/^\/api\/v1\/mcp\/?$/,
|
2026-05-28 22:08:49 +02:00
|
|
|
/^\/api\/v1\/docs\/?$/,
|
|
|
|
|
/^\/api\/v1\/openapi\/?$/,
|
2026-05-25 15:36:33 +02:00
|
|
|
/^\/api\/v1\/agents\/?$/,
|
|
|
|
|
/^\/api\/v1\/agents\/[^/]+\/?$/,
|
2026-05-27 22:02:22 +02:00
|
|
|
/^\/api\/v1\/agents\/[^/]+\/key\/?$/,
|
2026-05-25 15:36:33 +02:00
|
|
|
/^\/api\/v1\/databases\/?$/,
|
|
|
|
|
/^\/api\/v1\/databases\/[^/]+\/?$/,
|
|
|
|
|
/^\/api\/v1\/databases\/[^/]+\/backup\/?$/,
|
|
|
|
|
/^\/api\/v1\/databases\/[^/]+\/backup\/[^/]+\/?$/,
|
|
|
|
|
/^\/api\/v1\/databases\/[^/]+\/restore\/?$/,
|
|
|
|
|
/^\/api\/v1\/databases\/[^/]+\/status\/?$/,
|
2026-02-25 09:58:15 +01:00
|
|
|
];
|
|
|
|
|
return routePatterns.some((pattern) => pattern.test(pathname));
|
2024-11-11 18:50:42 +01:00
|
|
|
}
|
2024-11-28 20:28:00 +01:00
|
|
|
|
2024-11-11 18:50:42 +01:00
|
|
|
export const config = {
|
2026-02-25 09:58:15 +01:00
|
|
|
matcher: ["/api/:path*", "/dashboard/:path*", "/dashboard"],
|
2025-05-16 15:54:17 +02:00
|
|
|
};
|