11"use client" ;
22import Link from "next/link" ;
3- import { useState } from "react" ;
3+ import { useState , useRef } from "react" ;
4+ import HCaptcha from "@hcaptcha/react-hcaptcha" ;
45import { zodResolver } from "@hookform/resolvers/zod" ;
56import { useForm } from "react-hook-form" ;
67import { signin , signinWithOAuth } from "@/app/(auth)/actions" ;
8+ import { HCAPTCHA_SITE_KEY_PUBLIC } from "@/utils/constants" ;
79import { Provider } from "@supabase/supabase-js" ;
810import { Button } from "@/components/ui/button" ;
911import { Input } from "@/components/ui/input" ;
@@ -25,6 +27,8 @@ import { useToggle } from "@/hooks/useToggle";
2527export default function SignIn ( ) {
2628 const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
2729 const [ errorMessage , setErrorMessage ] = useState < string | null > ( null ) ;
30+ const [ captchaToken , setCaptchaToken ] = useState < string > ( ) ;
31+ const captcha = useRef < HCaptcha > ( null ) ;
2832 const searchParams = useSearchParams ( ) ;
2933 const callbackForDesktopApp = searchParams ?. get ( "callback" ) ?? "" ;
3034
@@ -42,9 +46,15 @@ export default function SignIn() {
4246 setErrorMessage ( null ) ;
4347
4448 try {
49+ if ( ! captchaToken ) {
50+ setErrorMessage ( "Please complete the CAPTCHA" ) ;
51+ return ;
52+ }
53+
4554 const formData = new FormData ( ) ;
4655 formData . append ( "email" , data . email ) ;
4756 formData . append ( "password" , data . password ) ;
57+ formData . append ( "captchaToken" , captchaToken ) ;
4858
4959 const response = await signin ( formData , callbackForDesktopApp ) ;
5060 if ( response ?. error ) {
@@ -56,6 +66,8 @@ export default function SignIn() {
5666 setErrorMessage ( "An unexpected error occurred. Please try again." ) ;
5767 } finally {
5868 setIsSubmitting ( false ) ;
69+ captcha . current ?. resetCaptcha ( ) ;
70+ setCaptchaToken ( undefined ) ;
5971 }
6072 } ;
6173
@@ -193,6 +205,17 @@ export default function SignIn() {
193205 Forgot Password?
194206 </ Link >
195207 </ div >
208+
209+ < div className = "flex justify-center" >
210+ < HCaptcha
211+ ref = { captcha }
212+ sitekey = { HCAPTCHA_SITE_KEY_PUBLIC }
213+ onVerify = { ( token ) => {
214+ setCaptchaToken ( token ) ;
215+ } }
216+ />
217+ </ div >
218+
196219 < Button
197220 type = "submit"
198221 size = "lg"
0 commit comments