Files
errors.bitreport.pt/admin-ui/middleware.ts
T
David Santos 8b0044be82 feat(api): CRM parity mutations and recent-errors filters (BIT-726)
Add POST /api/error-monitoring/resolve, bulk-resolve, and purge-resolved with
resolve key or admin JWT (errors:triage / errors:delete). Extend GET
recent-errors with status, severity, project_slug, q, offset, and stable
ordering. Document contracts in api/docs/ERROR_MONITORING_PUBLIC_API.md;
add vitest for public auth helpers. Proxy new routes and widen admin-ui
middleware for /api/error-monitoring/*.

Paperclip-Issue: BIT-726
Paperclip-Run: 3fbf92be-736e-4b11-8e90-cf993d8cd5ca
Paperclip-Agent: Pedro Alves (id d706f4e6-5d05-4cec-b6e8-f8fb1f203aae)
Made-with: Cursor
2026-04-02 10:50:06 +00:00

58 lines
1.7 KiB
TypeScript

import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
/** Admin session cookie checked by middleware. */
const ACCESS_COOKIE = "__Host-access";
/**
* Unauthenticated access is only allowed when demo mode is explicitly enabled in development.
* Edge middleware often sees `NODE_ENV` unset; treating "not production" as dev would open the shell publicly if DEMO=1.
* Opt-in escape hatch: ERROR_REPORTING_UI_SKIP_LOGIN=1 (local/staging only; never in public prod).
*/
function _allowWithoutSession(): boolean {
if (process.env.ERROR_REPORTING_UI_SKIP_LOGIN === "1") {
return true;
}
return (
process.env.NODE_ENV === "development" &&
process.env.ERROR_REPORTING_UI_DEMO === "1"
);
}
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
/** Paperclip + CRM contract: JSON with api_key on Fastify; proxy routes must not 307 to /login. */
if (pathname.startsWith("/api/error-monitoring/")) {
return NextResponse.next();
}
if (_allowWithoutSession()) {
return NextResponse.next();
}
if (
pathname.startsWith("/login") ||
pathname.startsWith("/_next") ||
pathname === "/favicon.ico"
) {
return NextResponse.next();
}
if (!request.cookies.get(ACCESS_COOKIE)?.value) {
const _url = request.nextUrl.clone();
_url.pathname = "/login";
const _next = pathname + request.nextUrl.search;
if (_next && _next !== "/login") {
_url.searchParams.set("next", _next);
}
return NextResponse.redirect(_url);
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!_next/static|_next/image|.*\\.(?:ico|png|jpg|jpeg|svg|webp)$).*)"],
};