Files
portabase/middleware.ts
T

71 lines
2.4 KiB
TypeScript
Raw Normal View History

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";
2024-11-11 18:50:42 +01:00
2025-05-16 15:54:17 +02:00
export async function middleware(request: NextRequest) {
2024-11-11 18:50:42 +01:00
const url = request.nextUrl.clone();
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) {
return NextResponse.redirect(new URL("/login", request.url));
}
2024-11-28 20:28:00 +01:00
return NextResponse.next();
}
2025-05-16 15:54:17 +02:00
// Exclude `/api/auth` and its subpaths
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 the route does not exist, return a 404 JSON response
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
}
}
// Function to check if the route exists (supports dynamic routes)
2025-05-16 15:54:17 +02:00
function checkRouteExists(pathname: string) {
2024-11-11 18:50:42 +01:00
// Define static and dynamic routes with patterns
const routePatterns = [
//do not delete
// /^\/api\/auth\/\d+$/, // Dynamic route with a number as a parameter (e.g., /api/dynamic/123)
// /^\/api\/auth\/\w+$/, // Dynamic route with a number as a parameter (e.g., /api/dynamic/123)
// /^\/api\/agent\/healthcheck\/\w+$/, // Dynamic route with an alphanumeric parameter (e.g., /api/user/username)
2025-05-16 15:54:17 +02:00
/^\/api\/agent\/[^/]+\/status\/?$/, // Dynamic route for /api/agent/[id]/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\/[^/]+\/?$/,
/^\/api\/images\/[^/]+\/?$/,
/^\/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 = {
2025-05-16 15:54:17 +02:00
runtime: "nodejs",
2024-11-28 20:28:00 +01:00
matcher: [
// '/api/agent/:path*',
2025-05-16 15:54:17 +02:00
"/api/:path*",
"/dashboard/:path*",
"/dashboard",
2024-11-28 20:28:00 +01:00
],
2025-05-16 15:54:17 +02:00
};