11"use client" ;
22
3- import { ThemeContext } from "@components/contexts" ;
3+ import { ServerConfigContext , ThemeContext } from "@components/contexts" ;
44import {
55 Button ,
66 Caption ,
@@ -33,7 +33,7 @@ import {
3333import Link from "next/link" ;
3434import { TriangleAlert } from "lucide-react" ;
3535import { useRecaptcha } from "@/hooks/use-recaptcha" ;
36- import { useRouter } from "next/navigation " ;
36+ import RecaptchaScriptLoader from "@/components/recaptcha-script-loader " ;
3737
3838export default function LoginForm ( { redirectTo } : { redirectTo ?: string } ) {
3939 const { theme } = useContext ( ThemeContext ) ;
@@ -43,55 +43,77 @@ export default function LoginForm({ redirectTo }: { redirectTo?: string }) {
4343 const [ error , setError ] = useState ( "" ) ;
4444 const [ loading , setLoading ] = useState ( false ) ;
4545 const { toast } = useToast ( ) ;
46+ const serverConfig = useContext ( ServerConfigContext ) ;
4647 const { executeRecaptcha } = useRecaptcha ( ) ;
47- const router = useRouter ( ) ;
4848
4949 const requestCode = async function ( e : FormEvent ) {
5050 e . preventDefault ( ) ;
5151 setLoading ( true ) ;
5252 setError ( "" ) ;
5353
54- if ( ! executeRecaptcha ) {
55- toast ( {
56- title : TOAST_TITLE_ERROR ,
57- description : "reCAPTCHA service not available. Please try again later." ,
58- variant : "destructive" ,
59- } ) ;
60- setLoading ( false ) ;
61- return ;
62- }
63-
64- const recaptchaToken = await executeRecaptcha ( "login_code_request" ) ;
65- if ( ! recaptchaToken ) {
66- toast ( {
67- title : TOAST_TITLE_ERROR ,
68- description : "reCAPTCHA validation failed. Please try again." ,
69- variant : "destructive" ,
70- } ) ;
71- setLoading ( false ) ;
72- return ;
73- }
54+ if ( serverConfig . recaptchaSiteKey ) {
55+ if ( ! executeRecaptcha ) {
56+ toast ( {
57+ title : TOAST_TITLE_ERROR ,
58+ description :
59+ "reCAPTCHA service not available. Please try again later." ,
60+ variant : "destructive" ,
61+ } ) ;
62+ setLoading ( false ) ;
63+ return ;
64+ }
7465
75- try {
76- const recaptchaVerificationResponse = await fetch ( "/api/recaptcha" , {
77- method : "POST" ,
78- headers : { "Content-Type" : "application/json" } ,
79- body : JSON . stringify ( { token : recaptchaToken } ) ,
80- } ) ;
66+ const recaptchaToken = await executeRecaptcha ( "login_code_request" ) ;
67+ if ( ! recaptchaToken ) {
68+ toast ( {
69+ title : TOAST_TITLE_ERROR ,
70+ description :
71+ "reCAPTCHA validation failed. Please try again." ,
72+ variant : "destructive" ,
73+ } ) ;
74+ setLoading ( false ) ;
75+ return ;
76+ }
77+ try {
78+ const recaptchaVerificationResponse = await fetch (
79+ "/api/recaptcha" ,
80+ {
81+ method : "POST" ,
82+ headers : { "Content-Type" : "application/json" } ,
83+ body : JSON . stringify ( { token : recaptchaToken } ) ,
84+ } ,
85+ ) ;
8186
82- const recaptchaData = await recaptchaVerificationResponse . json ( ) ;
87+ const recaptchaData =
88+ await recaptchaVerificationResponse . json ( ) ;
8389
84- if ( ! recaptchaVerificationResponse . ok || ! recaptchaData . success || ( recaptchaData . score && recaptchaData . score < 0.5 ) ) {
90+ if (
91+ ! recaptchaVerificationResponse . ok ||
92+ ! recaptchaData . success ||
93+ ( recaptchaData . score && recaptchaData . score < 0.5 )
94+ ) {
95+ toast ( {
96+ title : TOAST_TITLE_ERROR ,
97+ description : `reCAPTCHA verification failed. ${ recaptchaData . score ? `Score: ${ recaptchaData . score . toFixed ( 2 ) } .` : "" } Please try again.` ,
98+ variant : "destructive" ,
99+ } ) ;
100+ setLoading ( false ) ;
101+ return ;
102+ }
103+ } catch ( err ) {
104+ console . error ( "Error during reCAPTCHA verification:" , err ) ;
85105 toast ( {
86106 title : TOAST_TITLE_ERROR ,
87- description : `reCAPTCHA verification failed. ${ recaptchaData . score ? `Score: ${ recaptchaData . score . toFixed ( 2 ) } .` : '' } Please try again.` ,
107+ description :
108+ "reCAPTCHA verification failed. Please try again." ,
88109 variant : "destructive" ,
89110 } ) ;
90111 setLoading ( false ) ;
91112 return ;
92113 }
114+ }
93115
94- // Proceed with code generation if reCAPTCHA is successful
116+ try {
95117 const url = `/api/auth/code/generate?email=${ encodeURIComponent (
96118 email ,
97119 ) } `;
@@ -113,8 +135,7 @@ export default function LoginForm({ redirectTo }: { redirectTo?: string }) {
113135 description : "An unexpected error occurred. Please try again." ,
114136 variant : "destructive" ,
115137 } ) ;
116- }
117- finally {
138+ } finally {
118139 setLoading ( false ) ;
119140 }
120141 } ;
@@ -131,11 +152,6 @@ export default function LoginForm({ redirectTo }: { redirectTo?: string }) {
131152 if ( response ?. error ) {
132153 setError ( `Can't sign you in at this time` ) ;
133154 } else {
134- // toast({
135- // title: TOAST_TITLE_SUCCESS,
136- // description: LOGIN_SUCCESS,
137- // });
138- // router.replace(redirectTo || "/dashboard/my-content");
139155 window . location . href = redirectTo || "/dashboard/my-content" ;
140156 }
141157 } finally {
@@ -151,7 +167,8 @@ export default function LoginForm({ redirectTo }: { redirectTo?: string }) {
151167 { error && (
152168 < div
153169 style = { {
154- color : theme ?. theme ?. colors ?. error ,
170+ color : theme ?. theme ?. colors ?. light
171+ ?. destructive ,
155172 } }
156173 className = "flex items-center gap-2 mb-4"
157174 >
@@ -270,6 +287,7 @@ export default function LoginForm({ redirectTo }: { redirectTo?: string }) {
270287 </ div >
271288 </ div >
272289 </ div >
290+ < RecaptchaScriptLoader />
273291 </ Section >
274292 ) ;
275293}
0 commit comments