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:
@@ -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 }}>
|
||||
|
||||
@@ -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}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
|
||||
Reference in New Issue
Block a user