11import { useEffect , useRef } from 'react' ;
22
3- const DEFAULT_SITE_KEY = import . meta. env . VITE_TURNSTILE_SITE_KEY as string ;
4-
5- declare global {
6- interface Window {
7- onTurnstileOK ?: ( token : string ) => void ;
8- onTurnstileError ?: ( err : unknown ) => void ;
9- }
10- }
3+ const TEST_SITE_KEY = import . meta. env . VITE_SITE_KEY as string ;
114
125export default function TurnstileBridge ( ) {
13- const containerRef = useRef < HTMLDivElement | null > ( null ) ;
14- const scriptLoadedRef = useRef ( false ) ;
6+ const containerRef = useRef < HTMLDivElement > ( null ) ;
157
168 useEffect ( ( ) => {
179 const urlParams = new URLSearchParams ( window . location . search ) ;
18- const siteKey = urlParams . get ( 'sitekey' ) || DEFAULT_SITE_KEY ;
19-
20- if ( ! siteKey ) {
21- console . error ( 'No sitekey provided' ) ;
22- return ;
23- }
24-
25- if ( scriptLoadedRef . current ) {
26- return ;
27- }
10+ const siteKey = urlParams . get ( 'sitekey' ) || TEST_SITE_KEY ;
11+
12+ console . log ( 'Turnstile sitekey:' , siteKey ) ;
2813
29- const existingScript = document . querySelector ( 'script[src*="turnstile"]' ) ;
30- if ( existingScript ) {
31- scriptLoadedRef . current = true ;
32- initializeTurnstile ( siteKey ) ;
14+ if ( document . querySelector ( 'script[src*="turnstile"]' ) ) {
15+ renderTurnstile ( siteKey ) ;
3316 return ;
3417 }
3518
3619 const script = document . createElement ( 'script' ) ;
3720 script . src = 'https://challenges.cloudflare.com/turnstile/v0/api.js' ;
3821 script . async = true ;
39- script . defer = true ;
40-
4122 script . onload = ( ) => {
42- scriptLoadedRef . current = true ;
43- initializeTurnstile ( siteKey ) ;
23+ console . log ( 'Turnstile script loaded' ) ;
24+ renderTurnstile ( siteKey ) ;
4425 } ;
45-
4626 script . onerror = ( ) => {
4727 console . error ( 'Failed to load Turnstile script' ) ;
4828 } ;
49-
29+
5030 document . head . appendChild ( script ) ;
51-
52- return ( ) => {
53- } ;
5431 } , [ ] ) ;
5532
56- const initializeTurnstile = ( siteKey : string ) => {
57- if ( ! containerRef . current ) {
58- console . error ( 'Container not found' ) ;
59- return ;
60- }
61-
62- if ( typeof ( window as any ) . turnstile === 'undefined' ) {
63- console . error ( 'Turnstile not available' ) ;
33+ const renderTurnstile = ( siteKey : string ) => {
34+ if ( ! containerRef . current || ! ( window as any ) . turnstile ) {
35+ console . error ( 'Container or Turnstile not available' ) ;
6436 return ;
6537 }
6638
6739 try {
40+ console . log ( 'Rendering Turnstile with sitekey:' , siteKey ) ;
41+
6842 ( window as any ) . turnstile . render ( containerRef . current , {
6943 sitekey : siteKey ,
7044 callback : ( token : string ) => {
71- try {
72- window . location . href =
73- 'ethoraappreactnative://turnstile?token=' +
74- encodeURIComponent ( token ) ;
75- } catch ( err ) {
76- console . error ( 'Error redirecting with token:' , err ) ;
77- }
45+ console . log ( 'Turnstile success, token:' , token ) ;
46+ window . location . href = `ethoraappreactnative://turnstile?token=${ encodeURIComponent ( token ) } ` ;
7847 } ,
7948 'error-callback' : ( error : string ) => {
80- try {
81- window . location . href =
82- 'ethoraappreactnative://turnstile?error=' +
83- encodeURIComponent ( String ( error ) ) ;
84- } catch ( err ) {
85- console . error ( 'Error redirecting with error:' , err ) ;
86- }
49+ console . error ( 'Turnstile error:' , error ) ;
50+ window . location . href = `ethoraappreactnative://turnstile?error=${ encodeURIComponent ( error ) } ` ;
8751 } ,
88- action : 'signup' ,
8952 theme : 'light' ,
9053 } ) ;
91- } catch ( err ) {
92- console . error ( 'Error initializing Turnstile:' , err ) ;
54+ } catch ( error ) {
55+ console . error ( 'Error rendering Turnstile:' , error ) ;
9356 }
9457 } ;
9558
9659 return (
97- < div
98- style = { {
99- minHeight : '100vh' ,
100- display : 'flex' ,
101- alignItems : 'center' ,
102- justifyContent : 'center' ,
103- backgroundColor : '#f5f5f5' ,
104- } }
105- >
106- < div
107- ref = { containerRef }
108- style = { {
60+ < div style = { {
61+ minHeight : '100vh' ,
62+ display : 'flex' ,
63+ alignItems : 'center' ,
64+ justifyContent : 'center' ,
65+ backgroundColor : '#f5f5f5' ,
66+ fontFamily : 'Arial, sans-serif'
67+ } } >
68+ < div style = { {
69+ textAlign : 'center' ,
70+ padding : '20px'
71+ } } >
72+ < h2 style = { { marginBottom : '20px' , color : '#333' } } >
73+ Подтвердите, что вы не робот
74+ </ h2 >
75+ < div ref = { containerRef } style = { {
10976 minHeight : '65px' ,
11077 minWidth : '300px' ,
111- } }
112- />
78+ display : 'flex' ,
79+ justifyContent : 'center'
80+ } } />
81+ < p style = { { marginTop : '20px' , color : '#666' , fontSize : '14px' } } >
82+ Завершите проверку для продолжения
83+ </ p >
84+ </ div >
11385 </ div >
11486 ) ;
11587}
0 commit comments