@@ -6,26 +6,33 @@ export async function getRecaptchaToken(action: string): Promise<string> {
66
77 window . grecaptcha . ready ( async ( ) => {
88 try {
9- // 🔥 Wait until reCAPTCHA client exists
10- const maxWaitMs = 3000 ; // 3 seconds max
9+ const maxWaitMs = 5000 ;
1110 const pollIntervalMs = 100 ;
1211 let waited = 0 ;
1312
1413 while ( waited < maxWaitMs ) {
1514 const clients = ( window . grecaptcha as any ) ?. getClients ?.( ) ;
1615 if ( clients && Object . keys ( clients ) . length > 0 ) {
17- break ; // clients exist, safe to proceed
16+ break ;
1817 }
1918 await new Promise ( ( r ) => setTimeout ( r , pollIntervalMs ) ) ;
2019 waited += pollIntervalMs ;
2120 }
2221
23- // Final check
22+ // If no clients exist after waiting, manually render a dummy invisible badge
2423 const clients = ( window . grecaptcha as any ) ?. getClients ?.( ) ;
2524 if ( ! clients || Object . keys ( clients ) . length === 0 ) {
26- return reject ( new Error ( 'No reCAPTCHA clients available after waiting.' ) ) ;
25+ console . warn ( 'No reCAPTCHA clients found after waiting. Manually creating client.' ) ;
26+ const dummyContainer = document . createElement ( 'div' ) ;
27+ dummyContainer . style . display = 'none' ;
28+ document . body . appendChild ( dummyContainer ) ;
29+ ( window . grecaptcha as any ) . render ( dummyContainer , {
30+ sitekey : import . meta. env . VITE_RECAPTCHA_SITE_KEY ,
31+ size : 'invisible' ,
32+ } ) ;
2733 }
2834
35+ // Now execute
2936 const token = await window . grecaptcha . execute ( import . meta. env . VITE_RECAPTCHA_SITE_KEY , {
3037 action,
3138 } ) ;
0 commit comments