/** * Unsubscribe endpoint referenced by the List-Unsubscribe header. * * - POST: RFC 8058 one-click. Mail providers POST `List-Unsubscribe=One-Click`. * We unsubscribe immediately and always answer 200 (never leak whether * the token was valid). No login, processed on the POST alone. * - GET: fallback for clients that follow the link; unsubscribes and shows a * minimal confirmation. */ import { NextRequest, NextResponse } from "next/server"; import { unsubscribeByToken } from "@/lib/data/recipients"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; async function process(token: string | null): Promise { if (token) { try { await unsubscribeByToken(token); } catch (err) { console.error("[unsubscribe] error", err); } } } export async function POST(req: NextRequest) { await process(req.nextUrl.searchParams.get("token")); return new NextResponse("You have been unsubscribed.", { status: 200, headers: { "Content-Type": "text/plain" }, }); } export async function GET(req: NextRequest) { await process(req.nextUrl.searchParams.get("token")); const html = ` Unsubscribed

You're unsubscribed

You will no longer receive marketing emails from Bests Offer. If this was a mistake, you can re-subscribe at bestsoffer.com.

`; return new NextResponse(html, { status: 200, headers: { "Content-Type": "text/html; charset=utf-8" }, }); }