Skip to content

Commit 470cbc6

Browse files
committed
fix turnstile
1 parent 1d9bb63 commit 470cbc6

File tree

3 files changed

+46
-144
lines changed

3 files changed

+46
-144
lines changed

src/components/ErrorBoundary.tsx

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/pages/TurnstileBridge.tsx

Lines changed: 44 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,87 @@
11
import { 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

125
export 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
}

src/router.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import LoginComponent from './pages/AuthPage/Login';
88
import Register from './pages/AuthPage/Register';
99
import Chat from './pages/Chat';
1010
import { Error404Page } from './pages/ErrorPage/Error404Page';
11-
import { ErrorBoundary } from './components/ErrorBoundary';
1211
const Admin = lazy(() => import('./pages/Admin'));
1312
const AdminApp = lazy(() => import('./pages/AdminApp'));
1413
const AdminApps = lazy(() => import('./pages/AdminApps'));
@@ -18,10 +17,10 @@ const AppUsers = lazy(() => import('./pages/AppUsers'));
1817
const Profile = lazy(() => import('./pages/Profile'));
1918
const UserSettings = lazy(() => import('./pages/UserSettings/UserSettings'));
2019
const ProfileEdit = lazy(() => import('./pages/ProfileEdit'));
21-
const TurnstileBridge = lazy(() => import('./pages/TurnstileBridge'));
2220

2321
import App from './App';
2422
import AdminLayout from './pages/AdminLayout';
23+
import TurnstileBridge from './pages/TurnstileBridge';
2524

2625
export const router = createBrowserRouter(
2726
[
@@ -50,11 +49,7 @@ export const router = createBrowserRouter(
5049
},
5150
{
5251
path: '/turnstile',
53-
element: (
54-
<ErrorBoundary>
55-
<TurnstileBridge />
56-
</ErrorBoundary>
57-
),
52+
Component: TurnstileBridge,
5853
},
5954
{
6055
path: '/app',

0 commit comments

Comments
 (0)