import { NextResponse, type NextRequest } from "next/server" import { getSessionCookie } from "better-auth/cookies" const PROTECTED_PATHS = [ "/admin", "/dashboard", "/properties", "/tenants", "/rent", "/maintenance", "/leases", "/expenses", "/settings", "/onboarding", "/calendar", "/inspections", "/vendors", "/reports", "/activity", "/ai", "/ai-dashboard", "/predictions", "/recommendations", "/impact", "/follow-ups", "/team", ] const AUTH_PATHS = ["/login", "/signup", "/forgot-password"] // Origin of the Sentry ingest endpoint, derived from the public DSN so the // CSP stays in sync with whatever project/region the DSN points at. Returns // null when Sentry is not configured. function sentryIngestOrigin(): string | null { const dsn = process.env.NEXT_PUBLIC_SENTRY_DSN if (!dsn) return null try { return new URL(dsn).origin } catch { return null } } // Origin serving the Umami analytics script (script.js) and receiving its event // beacons (POST /api/send). Mirrors the component default so the CSP allows both // loading the script AND sending events; stays in sync with NEXT_PUBLIC_UMAMI_SRC // when overridden. function umamiOrigin(): string { const src = process.env.NEXT_PUBLIC_UMAMI_SRC || "https://fickanalytics.phluit.net/script.js" try { return new URL(src).origin } catch { return "" } } // Build the Content-Security-Policy. // // `script-src` is NONCE-based on the app surface and 'unsafe-inline' elsewhere. // // The nonce is minted per request and set on the REQUEST headers, which is how // Next discovers it and stamps it onto its inline hydration scripts; we echo the // policy on the response. Browsers ignore 'unsafe-inline' once a nonce is // present, so an injected inline