- Add middleware redirecting unauthenticated users to /login unless dev+demo - Disable demo incident data when NODE_ENV=production - Forward __Host-access cookie on SSR fetches to admin API - Document NPM /admin/ proxy to Fastify api (§14.2.2) Co-Authored-By: Paperclip <[email protected]>
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import type { NextRequest } from "next/server";
|
|
import { NextResponse } from "next/server";
|
|
|
|
/** Matches Fastify admin auth cookie name (`api/src/plugins/auth.ts`). */
|
|
const ACCESS_COOKIE = "__Host-access";
|
|
|
|
/**
|
|
* Unauthenticated access is only allowed in non-production when demo mode is on.
|
|
* In production, demo data is disabled in `lib/incidents-data.ts` and routes require a session.
|
|
*/
|
|
function _allowWithoutSession(): boolean {
|
|
return (
|
|
process.env.NODE_ENV !== "production" && process.env.ERROR_REPORTING_UI_DEMO === "1"
|
|
);
|
|
}
|
|
|
|
export function middleware(request: NextRequest) {
|
|
const { pathname } = request.nextUrl;
|
|
|
|
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)$).*)"],
|
|
};
|