import "server-only"; import type { Session } from "next-auth"; import { NextResponse } from "next/server"; import { auth } from "@/auth"; type Handler = (req: Request, ctx: Ctx & { session: Session }) => Promise | Response; /** Wraps a route handler so it only runs for a logged-in user (defense in depth behind proxy.ts). */ export function withAuth(handler: Handler) { return async (req: Request, ctx: Ctx) => { const session = await auth(); if (!session?.user) return NextResponse.json({ error: "unauthorized" }, { status: 401 }); return handler(req, { ...ctx, session }); }; }