feat(auth): implement RBAC audit logging and session security
Integrate granular audit logging for admin actions, transition to role-based API access via `requireAdminApiSession`, and update session tokens to persist user roles and IDs for consistent multi-admin management.
This commit is contained in:
1 parent
2873b9904a
commit
5c977d6977
37 files changed
+5939
-349
No files matched your search
@@ -0,0 +1,41 @@
|
||||
import { listAdminAuditLogsAsync } from "@/core/auth/auditLog";
|
||||
import { requireAdminApiSession } from "@/lib/adminAuth";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const auth = await requireAdminApiSession(req, { minRole: "viewer" });
|
||||
if (!auth.valid) {
|
||||
return auth.response;
|
||||
}
|
||||
|
||||
try {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const rawLimit = Number(searchParams.get("limit") || "100");
|
||||
const limit = Number.isFinite(rawLimit)
|
||||
? Math.min(Math.max(Math.floor(rawLimit), 1), 500)
|
||||
: 100;
|
||||
const action = searchParams.get("action") || undefined;
|
||||
const actorEmail =
|
||||
searchParams.get("actor") ||
|
||||
searchParams.get("actorEmail") ||
|
||||
undefined;
|
||||
const search =
|
||||
searchParams.get("q") || searchParams.get("search") || undefined;
|
||||
|
||||
const logs = await listAdminAuditLogsAsync({
|
||||
limit,
|
||||
action,
|
||||
actorEmail,
|
||||
search,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
logs,
|
||||
total: logs.length,
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
const message =
|
||||
err instanceof Error ? err.message : "Unable to fetch audit logs.";
|
||||
return NextResponse.json({ error: message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user