Add Fastify GET /v1/paperclip/recent-errors returning CRM-compatible JSON (data envelope, project_slug per row, status open for open/investigating incidents). Admin UI proxies the public path /api/error-monitoring/recent-errors to the API base; middleware bypasses the session redirect for that path only. Auth matches CRM env names: ERROR_RESOLVE_API_KEY or ERROR_REPORTING_API_KEY for ?api_key=, DEBUG_VIEW_ERRORS_KEY for ?view_errors=. Paperclip-Agent: David Santos (urlKey david-santos; id 0b1fcf92-5fe3-4ac0-9f29-df4ab72f5376) Paperclip-Issue: BIT-684 Paperclip-Run: cf7cb964-6953-4476-af09-3feb5d10da17 Made-with: Cursor
58 lines
1.7 KiB
TypeScript
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 error loop + CRM contract: JSON GET with api_key; must not 307 to /login. */
|
|
if (pathname === "/api/error-monitoring/recent-errors") {
|
|
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)$).*)"],
|
|
};
|