2025-04-08 14:37:17 +07:00

43 lines
1.5 KiB
JavaScript

import { NextResponse } from "next/server";
import fetch from "node-fetch";
const sleep = () => new Promise((resolve) => setTimeout(resolve, 350));
export async function POST(request) {
const body = await request.json();
const { domain, email, captcha } = body;
const debugInfo = {
receivedData: { domain, email, captchaLength: captcha?.length },
secretKeyPrefix: process.env.RECAPTCHA_SECRET_KEY?.substring(0, 5) + "...",
};
if (!domain || !email || !captcha) {
return NextResponse.json({ message: "All fields are required", debug: debugInfo }, { status: 422 });
}
try {
const verifyUrl = `https://www.google.com/recaptcha/api/siteverify?secret=${process.env.RECAPTCHA_SECRET_KEY}&response=${captcha}`;
debugInfo.verifyUrl = verifyUrl;
const response = await fetch(verifyUrl, {
method: "POST",
});
const captchaValidation = await response.json();
debugInfo.captchaValidation = captchaValidation;
if (captchaValidation.success) {
await sleep();
return NextResponse.json({ message: "OK", debug: debugInfo }, { status: 200 });
}
return NextResponse.json({ message: "Captcha validation failed", debug: debugInfo }, { status: 422 });
} catch (error) {
console.error("Error submitting form:", error);
return NextResponse.json(
{ message: "Something went wrong", error: error.message, debug: debugInfo },
{ status: 422 }
);
}
}