2025-05-16 15:54:17 +02: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";
|
2025-07-26 16:34:06 +02:00
|
|
|
import { signOut } from "@/lib/auth/auth-client";
|
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) {
|
|
|
|
|
signOut();
|
|
|
|
|
return NextResponse.redirect(new URL("/login?error=banned", request.url));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (session.user.role === "pending") {
|
|
|
|
|
signOut();
|
|
|
|
|
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-05-16 15:54:17 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
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\/?$/,
|
2024-11-28 20:28:00 +01:00
|
|
|
/^\/api\/files\/[^/]+\/?$/,
|
2025-01-12 14:03:15 +01:00
|
|
|
/^\/api\/images\/[^/]+\/?$/,
|
2024-12-14 17:49:50 +01:00
|
|
|
/^\/api\/events\/?$/,
|
2025-01-03 20:48:58 +01:00
|
|
|
/^\/api\/init\/?$/,
|
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
|
|
|
};
|