---
title: React Turnstile - Form submission retry logic
---
> ## Documentation Index
> Fetch the complete documentation index at: https://docs.page/marsidev/react-turnstile/llms.txt
> Use this file to discover all available pages before exploring further.

# Form submission retry logic

Turnstile tokens are **single-use** and expire after 300 seconds: once your server validates a token with Siteverify, it can't be validated again. Every submission attempt needs a fresh token, including a retry after a failed login.

Reset the widget after each submission, and keep the submit button disabled until the new token arrives:

```tsx
"use client";
import { useRef, useState } from "react";
import { Turnstile } from "@marsidev/react-turnstile";
import type { TurnstileInstance } from "@marsidev/react-turnstile";

export default function LoginForm() {
  const turnstileRef = useRef<TurnstileInstance | null>(null);
  const [token, setToken] = useState<string | null>(null);
  const [error, setError] = useState<string | null>(null);

  async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault();
    const formData = new FormData(event.currentTarget);
    // This attempt consumes the token: disable the button until a new one arrives.
    const submittedToken = token;
    setToken(null);

    try {
      const res = await fetch("/api/login", {
        method: "POST",
        body: JSON.stringify({
          username: formData.get("username"),
          password: formData.get("password"),
          token: submittedToken,
        }),
        headers: { "content-type": "application/json" },
      });

      setError(res.ok ? null : "Login failed. Please try again.");
    } catch {
      setError("An unexpected error occurred. Please try again.");
    } finally {
      turnstileRef.current?.reset();
    }
  }

  return (
    <form onSubmit={handleSubmit}>
      <input type="text" name="username" placeholder="Username" />
      <input type="password" name="password" placeholder="Password" />
      {error && <p role="alert">{error}</p>}
      <Turnstile
        ref={turnstileRef}
        siteKey="{{ siteKey }}"
        options={{ action: "login" }}
        onSuccess={(token) => setToken(token)}
        onExpire={() => setToken(null)}
      />
      <button type="submit" disabled={!token}>
        Login
      </button>
    </form>
  );
}
```

With the default `managed` mode, most visitors get the new token automatically within a second or two, so resetting rarely asks anything of them.

Don't reuse a token across attempts or skip validation on retries. The server must validate a fresh token on every request to the protected endpoint; otherwise the retry path becomes a way around the challenge.

<Info>
  See [Validating a token](/validating-a-token) for the server side: `/api/login` validates the token itself before running the login logic.
</Info>
