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
This commit is contained in:
feco
2026-11-26 16:43:07 +00:00
co-authored by Claude Opus 5.5
parent fa46dcb757
commit 469f321ac0
14 changed files with 285 additions and 11 deletions
@@ -254,7 +254,9 @@ export default async function PrivacyPage() {
</li>
<li>
<strong>Cloudflare, Inc.</strong> – a forgalom továbbítása és a támadások elleni védelem; az oldal
letöltésekor kezeli az IP-címedet és a kérés adatait.
letöltésekor kezeli az IP-címedet és a kérés adatait. Az e-mailes regisztrációnál és a jelszókérésnél a
Turnstile szolgáltatásával ellenőrzi, hogy nem gép küldi az űrlapot (ehhez a böngésző jellemzőit is
vizsgálja).
</li>
<li>
<strong>Google LLC</strong> – bejelentkezés Google-fiókkal.
@@ -3,6 +3,7 @@
import { useRouter } from "next/navigation";
import { useState } from "react";
import { Turnstile } from "@/components/Turnstile";
import { ApiError, api, errorText } from "@/lib/api";
type Mode = "login" | "register" | "forgot";
@@ -25,9 +26,12 @@ export function PasswordLogin({
inviteToken = null,
referral = null,
initialMode = "login",
turnstileKey = null,
}: {
target: string;
inviteToken?: string | null;
/** Cloudflare Turnstile on the sign-up and reset forms (null: none). */
turnstileKey?: string | null;
/** The shared link the visitor came from (in the login link: `?ref=`), for the sharer's reward. */
referral?: string | null;
initialMode?: Mode;
@@ -41,6 +45,10 @@ export function PasswordLogin({
const [error, setError] = useState<string | null>(null);
const [sent, setSent] = useState<Mode | null>(null);
const [uninvited, setUninvited] = useState(false);
// Turnstile's token (sign-up and reset only); a new round after each try.
const [token, setToken] = useState<string | null>(null);
const [round, setRound] = useState(0);
const checked = mode === "login" || !turnstileKey;
function switchTo(next: Mode) {
setMode(next);
@@ -63,10 +71,17 @@ export function PasswordLogin({
}
if (mode === "register") {
await api("/auth/register", {
body: { email, display_name: name.trim() || null, locale: "hu", invite_token: inviteToken, referral },
body: {
email,
display_name: name.trim() || null,
locale: "hu",
invite_token: inviteToken,
referral,
turnstile_token: token,
},
});
} else {
await api("/auth/password-reset", { body: { email } });
await api("/auth/password-reset", { body: { email, turnstile_token: token } });
}
setSent(mode);
} catch (err) {
@@ -74,6 +89,8 @@ export function PasswordLogin({
else setError(mode === "login" ? loginError(err) : errorText(err));
} finally {
setBusy(false);
// A token works once.
if (mode !== "login" && turnstileKey) setRound((r) => r + 1);
}
}
@@ -156,14 +173,17 @@ export function PasswordLogin({
jelszót is beállíthatsz.
</p>
)}
<button type="submit" className="btn btn-primary" disabled={busy}>
{mode !== "login" && turnstileKey && <Turnstile siteKey={turnstileKey} onToken={setToken} round={round} />}
<button type="submit" className="btn btn-primary" disabled={busy || (!checked && !token)}>
{busy
? "Küldés…"
: mode === "login"
? "Belépés"
: mode === "register"
? "Regisztráció"
: "Link küldése"}
: !checked && !token
? "Egy pillanat…"
: mode === "login"
? "Belépés"
: mode === "register"
? "Regisztráció"
: "Link küldése"}
</button>
{error && (
<p className="msg msg-error" role="alert" style={{ margin: 0 }}>
+7 -1
View File
@@ -169,7 +169,13 @@ export default async function LoginPage({ searchParams }: { searchParams: Promis
</div>
)}
{password && (
<PasswordLogin target={target} inviteToken={pendingInvite} referral={referral} initialMode={initialMode} />
<PasswordLogin
target={target}
inviteToken={pendingInvite}
referral={referral}
initialMode={initialMode}
turnstileKey={config?.turnstile_site_key ?? null}
/>
)}
</>
) : (
+2 -1
View File
@@ -242,7 +242,8 @@ export default async function PrivacyPolicyPage() {
</li>
<li>
<strong>Cloudflare, Inc.</strong>: delivering traffic and protecting against attacks; processes your IP
address and request data when the site loads.
address and request data when the site loads. On the e-mail sign-up and password request forms its
Turnstile service checks that a person, not a script, sends the form (looking at browser features too).
</li>
<li>
<strong>Google LLC</strong>: signing in with a Google account.
@@ -0,0 +1,86 @@
"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" />;
}
+2
View File
@@ -98,6 +98,8 @@ export interface Question {
export interface PublicConfig {
google_login: { enabled: boolean; client_id: string | null };
/** Cloudflare Turnstile on the e-mail sign-up and reset forms (null: none). */
turnstile_site_key?: string | null;
/** Sign in with Apple; `client_id` is the Services ID. */
apple_login?: { enabled: boolean; client_id: string | null };
locales: string[];