2025-11-04 09:20:24 +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";
|
2024-11-11 18:50:42 +01:00
|
|
|
|
2025-11-03 11:37:54 +01:00
|
|
|
export async function proxy(request: NextRequest) {
|
2024-11-11 18:50:42 +01:00
|
|
|
const url = request.nextUrl.clone();
|
2025-07-26 16:34:06 +02:00
|
|
|
const redirectUrl = encodeURIComponent(request.nextUrl.pathname)
|
2024-11-28 20:28:00 +01:00
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
if (url.pathname.startsWith("/dashboard")) {
|
|
|
|
|
const session = await auth.api.getSession({
|
|
|
|
|
headers: await headers(),
|
|
|
|
|
});
|
|
|
|
|
if (!session) {
|
2025-07-26 16:34:06 +02:00
|
|
|
return NextResponse.redirect(new URL(`/login?redirect=${redirectUrl}`, request.url));
|
|
|
|
|
}
|
|
|
|
|
if (session.user.banned) {
|
2025-11-04 09:20:24 +01:00
|
|
|
await auth.api.signOut({headers: await headers()});
|
2025-07-26 16:34:06 +02:00
|
|
|
return NextResponse.redirect(new URL("/login?error=banned", request.url));
|
|
|
|
|
}
|
|
|
|
|
if (session.user.role === "pending") {
|
2025-11-04 09:20:24 +01:00
|
|
|
await auth.api.signOut({headers: await headers()});
|
2025-07-26 16:34:06 +02:00
|
|
|
return NextResponse.redirect(new URL(`/login?error=pending?redirect=${redirectUrl}`, request.url));
|
2025-05-16 15:54:17 +02:00
|
|
|
}
|
2025-07-27 10:23:56 +02:00
|
|
|
if (url.pathname === "/dashboard") {
|
|
|
|
|
return NextResponse.redirect(new URL(`/dashboard/home`, request.url));
|
|
|
|
|
}
|
2024-11-28 20:28:00 +01:00
|
|
|
return NextResponse.next();
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
if (url.pathname.startsWith("/api/auth")) {
|
|
|
|
|
return NextResponse.next();
|
|
|
|
|
}
|
2024-11-28 20:28:00 +01:00
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
if (url.pathname.startsWith("/api")) {
|
2024-11-11 18:50:42 +01:00
|
|
|
const routeExists = checkRouteExists(url.pathname);
|
|
|
|
|
if (!routeExists) {
|
2025-11-04 09:20:24 +01:00
|
|
|
return new NextResponse(JSON.stringify({message: "This API route does not exist.", status: 404}), {
|
2025-05-16 15:54:17 +02:00
|
|
|
status: 404,
|
2025-11-04 09:20:24 +01:00
|
|
|
headers: {"Content-Type": "application/json"},
|
2025-05-16 15:54:17 +02:00
|
|
|
});
|
2024-11-11 18:50:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-16 15:54:17 +02:00
|
|
|
try {
|
2024-11-11 18:50:42 +01:00
|
|
|
loggingMiddleware(request);
|
2025-05-16 15:54:17 +02:00
|
|
|
} 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) {
|
2024-11-11 18:50:42 +01:00
|
|
|
const routePatterns = [
|
2025-11-01 19:38:05 +01:00
|
|
|
/^\/api\/agent\/[^/]+\/status\/?$/,
|
2024-12-11 20:46:26 +01:00
|
|
|
/^\/api\/agent\/[^/]+\/backup\/?$/,
|
|
|
|
|
/^\/api\/agent\/[^/]+\/restore\/?$/,
|
2026-01-24 12:13:55 +01:00
|
|
|
/^\/api\/files\/images\/[^/]+\/?$/,
|
|
|
|
|
/^\/api\/files\/backups\/?$/,
|
2026-02-02 21:46:53 +01:00
|
|
|
/^\/api\/tus\/hooks\/?$/,
|
2024-12-14 17:49:50 +01:00
|
|
|
/^\/api\/events\/?$/,
|
2025-01-03 20:48:58 +01:00
|
|
|
/^\/api\/init\/?$/,
|
2025-11-04 08:48:46 +01:00
|
|
|
/^\/api\/config\/?$/,
|
2026-01-24 00:26:05 +01:00
|
|
|
/^\/api\/google\/drive\/callback\/?$/,
|
2024-11-11 18:50:42 +01:00
|
|
|
];
|
2025-05-16 15:54:17 +02: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 = {
|
2024-11-28 20:28:00 +01:00
|
|
|
matcher: [
|
2025-05-16 15:54:17 +02:00
|
|
|
"/api/:path*",
|
|
|
|
|
"/dashboard/:path*",
|
|
|
|
|
"/dashboard",
|
2025-11-03 11:37:54 +01:00
|
|
|
]
|
2025-05-16 15:54:17 +02:00
|
|
|
};
|