11"use client" ;
22import Link from "next/link" ;
3- import { useState } from "react" ;
3+ import { useState , useRef } from "react" ;
44import { signinWithOAuth } from "@/app/(auth)/actions" ;
55import { AdminUserAttributes , Provider } from "@supabase/supabase-js" ;
66import { zodResolver } from "@hookform/resolvers/zod" ;
@@ -22,9 +22,12 @@ import { useRouter } from "next/navigation";
2222import { Label } from "@/components/ui/label" ;
2323import { Checkbox } from "@/components/ui/checkbox" ;
2424import { useToggle } from "@/hooks/useToggle" ;
25+ import HCaptcha from "@hcaptcha/react-hcaptcha" ;
2526
2627export default function SignUp ( ) {
2728 const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
29+ const [ captchaToken , setCaptchaToken ] = useState < string | null > ( null ) ;
30+ const captcha = useRef < HCaptcha > ( null ) ;
2831 const form = useForm < SignUpFormData > ( {
2932 resolver : zodResolver ( signUpSchema ) ,
3033 defaultValues : {
@@ -42,14 +45,20 @@ export default function SignUp() {
4245 setIsSubmitting ( true ) ;
4346
4447 try {
45- const formData : AdminUserAttributes = {
48+ if ( ! captchaToken ) {
49+ toast . error ( "Please complete the captcha challenge" ) ;
50+ return ;
51+ }
52+
53+ const formData = {
4654 email : data . email ,
4755 password : data . password ,
4856 user_metadata : {
4957 full_name : data . full_name ,
5058 company_name : data . company_name ,
5159 heard_about_us : data . heard_about_us ,
5260 } ,
61+ captchaToken : captchaToken ,
5362 } ;
5463 const response = await fetch ( "/api/signup" , {
5564 method : "POST" ,
@@ -73,6 +82,8 @@ export default function SignUp() {
7382 toast . error ( "An unexpected error occurred. Please try again." ) ;
7483 } finally {
7584 setIsSubmitting ( false ) ;
85+ captcha . current ?. resetCaptcha ( ) ;
86+ setCaptchaToken ( null ) ;
7687 }
7788 } ;
7889
@@ -252,6 +263,14 @@ export default function SignUp() {
252263 ) }
253264 />
254265
266+ < HCaptcha
267+ ref = { captcha }
268+ sitekey = "fa6c8c52-7694-45b0-97ec-7814072256b4"
269+ onVerify = { ( token ) => {
270+ setCaptchaToken ( token ) ;
271+ } }
272+ />
273+
255274 < div className = "text-center text-sm text-gray-600" >
256275 < Link
257276 href = "/privacy"
0 commit comments