Files
portabase/proxy.ts
T

109 lines
3.2 KiB
TypeScript
Raw Normal View History

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";
const OPENAPI_ENABLED = String(env.OPENAPI_ENABLED).toLowerCase() === "true";
2024-11-11 18:50:42 +01:00
2025-11-03 11:37:54 +01:00
export async function proxy(request: NextRequest) {
const url = request.nextUrl.clone();
const redirectUrl = encodeURIComponent(request.nextUrl.pathname);
2024-11-28 20:28:00 +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) {
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") {
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
if (url.pathname.startsWith("/api/auth")) {
return NextResponse.next();
}
2024-11-28 20:28:00 +01:00
if (url.pathname.startsWith("/api")) {
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
}
}
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) {
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\/?$/,
/^\/api\/google\/drive\/callback\/?$/,
// v1 external API
2026-05-28 20:44:27 +02:00
...(OPENAPI_ENABLED
? [
/^\/api\/v1\/docs\/?$/,
/^\/api\/v1\/openapi\/?$/,
]
: []),
/^\/api\/v1\/agents\/?$/,
/^\/api\/v1\/agents\/[^/]+\/?$/,
2026-05-27 22:02:22 +02:00
/^\/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\/?$/,
];
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 = {
matcher: ["/api/:path*", "/dashboard/:path*", "/dashboard"],
2025-05-16 15:54:17 +02:00
};