Files
sorsfonalak/services/frontend/src/components/Turnstile.tsx
T
fecoandClaude Opus 5.5 469f321ac0 Cloudflare Turnstile on e-mail sign-up and password reset, and a limit per address
- The sign-up and "forgot password" forms carry a token from Cloudflare
  Turnstile's widget (managed, shown only when Cloudflare asks for a
  click); the backend checks it before an e-mail goes out. Off without
  TURNSTILE_SITE_KEY and TURNSTILE_SECRET_KEY (development); the site key
  comes to the page through /v1/config.
- At most ten such e-mail requests an hour per visitor address, so a
  script cannot make the site mail thousands of strangers.
- The privacy notice (HU, EN) mentions Turnstile with Cloudflare.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-26 16:43:07 +00:00

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" />;
}