Skip to content

Commit a0d6c21

Browse files
committed
fix
1 parent 2762696 commit a0d6c21

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

src/pages/TurnstileBridge.tsx

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { useEffect, useRef } from 'react';
22

3-
const SITE_KEY = import.meta.env.VITE_TURNSTILE_SITE_KEY!;
3+
const SITE_KEY = import.meta.env.VITE_TURNSTILE_SITE_KEY as string;
44

55
declare global {
66
interface Window {
7-
turnstile?: any;
87
onTurnstileOK?: (token: string) => void;
98
onTurnstileError?: (err: unknown) => void;
109
}
@@ -14,38 +13,49 @@ export default function TurnstileBridge() {
1413
const containerRef = useRef<HTMLDivElement | null>(null);
1514

1615
useEffect(() => {
17-
window.onTurnstileOK = (token: string) => {
18-
window.location.href =
19-
'ethoraappreactnative://turnstile?token=' + encodeURIComponent(token);
20-
};
21-
22-
window.onTurnstileError = (err: unknown) => {
23-
window.location.href =
24-
'ethoraappreactnative://turnstile?error=' +
25-
encodeURIComponent(String(err));
26-
};
27-
2816
const script = document.createElement('script');
2917
script.src = 'https://challenges.cloudflare.com/turnstile/v0/api.js';
3018
script.async = true;
3119
script.defer = true;
3220
script.onload = () => {
33-
if (containerRef.current && window.turnstile) {
34-
window.turnstile.render(containerRef.current, {
21+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22+
const ts = (window as any).turnstile as
23+
| {
24+
render: (
25+
el: HTMLElement,
26+
opts: {
27+
sitekey: string;
28+
callback?: (token: string) => void;
29+
'error-callback'?: (error: string) => void;
30+
action?: string;
31+
theme?: 'light' | 'dark' | 'auto';
32+
}
33+
) => void;
34+
}
35+
| undefined;
36+
37+
if (containerRef.current && ts) {
38+
ts.render(containerRef.current, {
3539
sitekey: SITE_KEY,
36-
callback: 'onTurnstileOK',
37-
'error-callback': 'onTurnstileError',
40+
callback: (token: string) => {
41+
window.location.href =
42+
'ethoraappreactnative://turnstile?token=' +
43+
encodeURIComponent(token);
44+
},
45+
'error-callback': (error: string) => {
46+
window.location.href =
47+
'ethoraappreactnative://turnstile?error=' +
48+
encodeURIComponent(String(error));
49+
},
3850
action: 'signup',
3951
theme: 'light',
4052
});
4153
}
4254
};
43-
document.head.appendChild(script);
4455

56+
document.head.appendChild(script);
4557
return () => {
4658
document.head.removeChild(script);
47-
delete window.onTurnstileOK;
48-
delete window.onTurnstileError;
4959
};
5060
}, []);
5161

0 commit comments

Comments
 (0)