From 37bfe9381b9a19d3554a056d40f5326fac95c04f Mon Sep 17 00:00:00 2001 From: kamini08 Date: Sun, 22 Sep 2024 14:15:04 +0530 Subject: [PATCH 1/2] add recaptcha to pbctf --- app/(default)/api/registration/pbctf/route.ts | 64 ++++++++++++++++ app/(default)/pbctf/page.tsx | 14 ++-- app/(default)/recruitment/page.tsx | 2 +- app/layout.tsx | 2 +- components/forms/pbctfForm.tsx | 27 +++++++ components/forms/recruitmentForm.tsx | 22 ++++-- components/forms/success.tsx | 76 ++++++++----------- 7 files changed, 145 insertions(+), 62 deletions(-) create mode 100644 app/(default)/api/registration/pbctf/route.ts diff --git a/app/(default)/api/registration/pbctf/route.ts b/app/(default)/api/registration/pbctf/route.ts new file mode 100644 index 0000000..cdef1b5 --- /dev/null +++ b/app/(default)/api/registration/pbctf/route.ts @@ -0,0 +1,64 @@ +import { db } from "@/Firebase"; + +import { recruitValidate } from "@/lib/server/utils"; + +import { + addDoc, + collection, + getDocs, + limit, + query, + where, +} from "firebase/firestore"; +import { NextResponse } from "next/server"; + +// Add a new registration +export async function POST(request: Request) { + const formData = await request.json(); + const { recaptcha_token } = formData; + + + const recaptchaToken = recaptcha_token; + + const details = { + event: { + token: recaptchaToken, + siteKey: process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY, + }, + }; + + + if (!recaptchaToken) { + return NextResponse.json( + { + message: "reCAPTCHA token not found! Try again", + error: "reCAPTCHA token not found!", + }, + { + status: 500, + } + ); + } + + // Verify the reCATPTCHA token + + const recaptchaResponse = await fetch( + `https://recaptchaenterprise.googleapis.com/v1/projects/${process.env.RECAPTCHA_PROJECT}/assessments?key=${process.env.RECAPTCHA_API_KEY}`, + { + method: "POST", + body: JSON.stringify(details), + } + ); + + const recaptchaResult = await recaptchaResponse.json(); + console.log(recaptchaResult.riskAnalysis.score); + if (recaptchaResult.riskAnalysis.score < 0.7) { + return NextResponse.json({ + message: "reCAPTCHA validation failed", + error: recaptchaResult["error-codes"], + }); + } + + // Return a response + return NextResponse.json({ message: "Recaptcha validated!" }); +} diff --git a/app/(default)/pbctf/page.tsx b/app/(default)/pbctf/page.tsx index a5c92c4..f0749f7 100644 --- a/app/(default)/pbctf/page.tsx +++ b/app/(default)/pbctf/page.tsx @@ -10,13 +10,13 @@ import { useRouter } from "next/navigation"; const PBCTFRegisterPage = () => { const router = useRouter(); - useEffect(() => { - onAuthStateChanged(auth, (user) => { - if (!user) { - router.push("/login"); - } - }); - }, [router]); + // useEffect(() => { + // onAuthStateChanged(auth, (user) => { + // if (!user) { + // router.push("/login"); + // } + // }); + // }, [router]); return (
diff --git a/app/(default)/recruitment/page.tsx b/app/(default)/recruitment/page.tsx index e5d8869..388de15 100644 --- a/app/(default)/recruitment/page.tsx +++ b/app/(default)/recruitment/page.tsx @@ -17,7 +17,7 @@ const RegisterPage = () => { // router.push("/login"); // } // }); - // });\ + // }); // useEffect(() => { // router.push("/"); // }) diff --git a/app/layout.tsx b/app/layout.tsx index d34e65f..fdb5920 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -29,7 +29,7 @@ export default function RootLayout({