import { NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";

/**
 * Check auth for routes excluded from middleware (large upload routes).
 * Returns null if authenticated, or a 401 NextResponse if not.
 */
export async function requireAuth(
  req: NextRequest,
): Promise<NextResponse | null> {
  // Skip in development
  if (process.env.NODE_ENV === "development") return null;

  const token = await getToken({
    req,
    secret: process.env.NEXTAUTH_SECRET,
  });

  if (token) return null;

  return NextResponse.json(
    { error: "Authentication required" },
    { status: 401 },
  );
}
