From 09187fa5ac6ff5a9d02aa662b9fd0f5c7a4fefd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Tue, 3 Oct 2023 00:26:51 -0400 Subject: [PATCH] feat(api): add captcha verify utility --- lib/server/middlewareButNotReally/captcha.ts | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lib/server/middlewareButNotReally/captcha.ts diff --git a/lib/server/middlewareButNotReally/captcha.ts b/lib/server/middlewareButNotReally/captcha.ts new file mode 100644 index 0000000..3c164c7 --- /dev/null +++ b/lib/server/middlewareButNotReally/captcha.ts @@ -0,0 +1,21 @@ +import { H3Event, EventHandlerRequest } from "h3"; +export default async function (ev: H3Event) { + 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", + }); + } +}