import { NextRequest, NextResponse } from "next/server"; // Better Auth's session cookie name (default prefix "better-auth"); the // "__Secure-" variant is used when cookies are served over HTTPS in production. const SESSION_COOKIES = ["better-auth.session_token", "__Secure-better-auth.session_token"]; // Authed surfaces that require an optimistic session-cookie check. Anonymous users // hitting these are redirected to /sign-in. Public/marketing/auth routes are NOT // listed here, so they are never redirected (CSP still applies to them, below). const AUTHED_PREFIXES = [ "/dashboard", "/episodes", "/series", "/usage", "/billing", "/team", "/api-keys", "/settings", "/admin", ]; /** * Runs on every request (see matcher). Two responsibilities: * * 1. CSP/nonce (all routes): generate a per-request base64 nonce with the Web Crypto * API (Edge-safe — no node:crypto), expose it on the inbound `x-nonce` request * header, and set a nonce-based Content-Security-Policy response header. Next.js * auto-applies this nonce to its own framework