@paroicms/site-generator-plugin
Version:
ParoiCMS Site Generator Plugin
21 lines (20 loc) • 847 B
JavaScript
import { ApiError } from "@paroicms/public-server-lib";
export async function validateRecaptchaResponse(service, { gRecaptchaResponse, secretKey, }) {
if (!gRecaptchaResponse)
throw new ApiError("Missing gRecaptchaResponse", 400);
if (!secretKey) {
throw new Error("Invalid configuration, missing 'recaptchaPrivateKey'");
}
const url = `https://www.google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${gRecaptchaResponse}`;
const response = await fetch(url, {
method: "post",
});
if (response.status < 200 || response.status >= 300) {
throw new Error("Failed to call the Google Recaptcha API");
}
const result = (await response.json());
if (result.hostname !== service.fqdn) {
throw new ApiError("Unauthorized", 401);
}
return result.success;
}