87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useEffect, useRef } from "react";
|
||
|
|
|
||
|
|
type TurnstileApi = {
|
||
|
|
render: (el: HTMLElement, options: Record<string, unknown>) => string;
|
||
|
|
reset: (id: string) => void;
|
||
|
|
remove: (id: string) => void;
|
||
|
|
};
|
||
|
|
|
||
|
|
declare global {
|
||
|
|
interface Window {
|
||
|
|
turnstile?: TurnstileApi;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const SCRIPT = "https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit";
|
||
|
|
let loading: Promise<TurnstileApi> | null = null;
|
||
|
|
|
||
|
|
function load(): Promise<TurnstileApi> {
|
||
|
|
if (window.turnstile) return Promise.resolve(window.turnstile);
|
||
|
|
loading ??= new Promise((resolve, reject) => {
|
||
|
|
const script = document.createElement("script");
|
||
|
|
script.src = SCRIPT;
|
||
|
|
script.async = true;
|
||
|
|
script.onload = () => (window.turnstile ? resolve(window.turnstile) : reject(new Error("turnstile")));
|
||
|
|
script.onerror = () => {
|
||
|
|
loading = null;
|
||
|
|
reject(new Error("turnstile"));
|
||
|
|
};
|
||
|
|
document.head.appendChild(script);
|
||
|
|
});
|
||
|
|
return loading;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Cloudflare Turnstile ("nem robot vagy?"): usually invisible, it shows a box
|
||
|
|
* only when Cloudflare wants a click. `onToken` gets the token (null when it
|
||
|
|
* expires); a new `round` asks for a fresh one (a token works once).
|
||
|
|
*/
|
||
|
|
export function Turnstile({
|
||
|
|
siteKey,
|
||
|
|
onToken,
|
||
|
|
round = 0,
|
||
|
|
}: {
|
||
|
|
siteKey: string;
|
||
|
|
onToken: (token: string | null) => void;
|
||
|
|
round?: number;
|
||
|
|
}) {
|
||
|
|
const box = useRef<HTMLDivElement>(null);
|
||
|
|
const widget = useRef<string | null>(null);
|
||
|
|
const callback = useRef(onToken);
|
||
|
|
callback.current = onToken;
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
let gone = false;
|
||
|
|
load()
|
||
|
|
.then((api) => {
|
||
|
|
if (gone || !box.current || widget.current) return;
|
||
|
|
widget.current = api.render(box.current, {
|
||
|
|
sitekey: siteKey,
|
||
|
|
appearance: "interaction-only",
|
||
|
|
language: "hu",
|
||
|
|
theme: "auto",
|
||
|
|
callback: (token: string) => callback.current(token),
|
||
|
|
"expired-callback": () => callback.current(null),
|
||
|
|
"error-callback": () => callback.current(null),
|
||
|
|
});
|
||
|
|
})
|
||
|
|
.catch(() => callback.current(null));
|
||
|
|
return () => {
|
||
|
|
gone = true;
|
||
|
|
if (widget.current && window.turnstile) window.turnstile.remove(widget.current);
|
||
|
|
widget.current = null;
|
||
|
|
};
|
||
|
|
}, [siteKey]);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
if (round > 0 && widget.current && window.turnstile) {
|
||
|
|
callback.current(null);
|
||
|
|
window.turnstile.reset(widget.current);
|
||
|
|
}
|
||
|
|
}, [round]);
|
||
|
|
|
||
|
|
return <div ref={box} className="turnstile" />;
|
||
|
|
}
|