import { NextResponse, type NextRequest } from "next/server"; import { ADMIN_COOKIE, verifySessionToken } from "@/lib/session"; // Gate every /admin route behind a valid session, except the login page and its // POST action. Runs on the Edge runtime, so it relies only on Web Crypto // (see lib/session.ts) — no Node APIs. export async function proxy(req: NextRequest) { const { pathname } = req.nextUrl; if (pathname === "/admin/login") return NextResponse.next(); const token = req.cookies.get(ADMIN_COOKIE)?.value; if (await verifySessionToken(token)) return NextResponse.next(); const url = req.nextUrl.clone(); url.pathname = "/admin/login"; url.searchParams.set("next", pathname); return NextResponse.redirect(url); } export const config = { matcher: ["/admin/:path*"], };