- Added `auditLogEdge.js` for Edge Runtime compatible audit logging, including console logging and API fallback. - Introduced `auditLogSafe.js` for safe audit logging without direct database imports, ensuring compatibility across runtimes. - Enhanced `auth.js` to integrate safe audit logging for login actions, including success and failure cases. - Created middleware `auditLog.js` to facilitate audit logging for API routes with predefined configurations. - Updated `middleware.js` to allow API route access without authentication checks. - Added tests for audit logging functionality and Edge compatibility in `test-audit-logging.mjs` and `test-edge-compatibility.mjs`. - Implemented safe audit logging tests in `test-safe-audit-logging.mjs` to verify functionality across environments.
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
import { auth } from "@/lib/auth";
|
|
|
|
export default auth((req) => {
|
|
const { pathname } = req.nextUrl;
|
|
|
|
// Allow access to auth pages
|
|
if (pathname.startsWith("/auth/")) {
|
|
return;
|
|
}
|
|
|
|
// Allow access to API routes (they handle their own auth)
|
|
if (pathname.startsWith("/api/")) {
|
|
return;
|
|
}
|
|
|
|
// Require authentication for all other pages
|
|
if (!req.auth) {
|
|
const url = new URL("/auth/signin", req.url);
|
|
url.searchParams.set("callbackUrl", req.nextUrl.pathname);
|
|
return Response.redirect(url);
|
|
}
|
|
|
|
// Check admin routes (role check only, no database access)
|
|
if (pathname.startsWith("/admin/")) {
|
|
if (!["admin", "project_manager"].includes(req.auth.user.role)) {
|
|
return Response.redirect(new URL("/", req.url));
|
|
}
|
|
}
|
|
});
|
|
|
|
export const config = {
|
|
matcher: [
|
|
/*
|
|
* Match all request paths except for the ones starting with:
|
|
* - api (all API routes handle their own auth)
|
|
* - _next/static (static files)
|
|
* - _next/image (image optimization files)
|
|
* - favicon.ico (favicon file)
|
|
* - auth pages (auth pages should be accessible)
|
|
*/
|
|
"/((?!api|_next/static|_next/image|favicon.ico|auth).*)",
|
|
],
|
|
};
|