import "server-only"; import { NextResponse } from "next/server"; import type { ApiResult } from "@/app/lib/types"; /** Build the error response for a failed ApiResult. * 401 is reserved for expired/absent session (the client redirects to /login); * all other failures (bad credentials, validation, business rules) use 400 so * the page can show the message inline. */ export function errorResponse(r: Extract, { ok: false }>): NextResponse { if (r.phoneVerificationRequired) { return NextResponse.json({ error: r.error, phoneVerificationRequired: true }, { status: 403 }); } return NextResponse.json({ error: r.error }, { status: r.expired ? 401 : 400 }); } export function unauthorized(message = "Not authenticated"): NextResponse { return NextResponse.json({ error: message }, { status: 401 }); } export function badRequest(message: string, extra?: Record): NextResponse { return NextResponse.json({ error: message, ...(extra ?? {}) }, { status: 400 }); }