Commit 9257b534
| SHA | 9257b534f76a967f068bbd8861145c519f81c7b4 |
|---|---|
| Author | Mohamed Bassem <me at mbassem dot com> |
| Author Date | 2025-11-30 11:38 +0000 |
| Committer | Mohamed Bassem <me at mbassem dot com> |
| Commit Date | 2025-11-30 11:38 +0000 |
| Parent(s) | 9a6d36f2e93c (diff) |
| Tree | f80b20766c1f |
fix: regen turnstile token on signup resubmission
| File | + | - | Graph | |
|---|---|---|---|---|
| M | apps/web/components/signup/SignUpForm.tsx | +14 | -1 |
|
1 file(s) changed, 14 insertions(+), 1 deletions(-)
apps/web/components/signup/SignUpForm.tsx
diff --git a/apps/web/components/signup/SignUpForm.tsx b/apps/web/components/signup/SignUpForm.tsx index bd4a1cf2..340b461a 100644 --- a/apps/web/components/signup/SignUpForm.tsx +++ b/apps/web/components/signup/SignUpForm.tsx @@ -1,6 +1,7 @@ "use client"; -import { useState } from "react"; +import type { TurnstileInstance } from "@marsidev/react-turnstile"; +import { useRef, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { ActionButton } from "@/components/ui/action-button"; @@ -51,6 +52,7 @@ export default function SignUpForm() { const router = useRouter(); const clientConfig = useClientConfig(); const turnstileSiteKey = clientConfig.turnstile?.siteKey; + const turnstileRef = useRef<TurnstileInstance>(null); const createUserMutation = api.users.create.useMutation(); @@ -114,6 +116,11 @@ export default function SignUpForm() { if (e instanceof TRPCClientError) { setErrorMessage(e.message); } + // Reset turnstile widget on error to get a new token + if (turnstileSiteKey) { + turnstileRef.current?.reset(); + form.setValue("turnstileToken", ""); + } return; } const resp = await signIn("credentials", { @@ -131,6 +138,11 @@ export default function SignUpForm() { resp?.error ?? "Hit an unexpected error while signing in", ); } + // Reset turnstile widget on error to get a new token + if (turnstileSiteKey) { + turnstileRef.current?.reset(); + form.setValue("turnstileToken", ""); + } return; } router.replace("/"); @@ -225,6 +237,7 @@ export default function SignUpForm() { <FormLabel>Verification</FormLabel> <FormControl> <Turnstile + ref={turnstileRef} siteKey={turnstileSiteKey} onSuccess={(token) => { field.onChange(token);