next/lib/server/middlewareButNotReally/captcha.ts

22 lines
509 B
TypeScript
Raw Normal View History

2023-10-03 00:26:51 -04:00
import { H3Event, EventHandlerRequest } from "h3";
export default async function (ev: H3Event<EventHandlerRequest>) {
const body = await readBody(ev);
let { data: cres }: { data: any } = await useFetch(
"https://www.google.com/recaptcha/api/siteverify",
{
method: "post",
body: {
secret: useRuntimeConfig().captcha.secret,
response: body["g-recaptcha-response"],
},
},
);
if (!cres.value.success) {
throw createError({
statusCode: 400,
message: "bad recaptcha",
});
}
}