Form attachments: reference smoke test with a mail sink, Turnstile reset fix, docs

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-13 16:42:39 +01:00
co-authored by Claude Fable 5.2
parent 387debc88c
commit fdb6140c09
13 changed files with 251 additions and 23 deletions
+15 -1
View File
@@ -29,6 +29,12 @@ function ensureScript(): void {
export type TurnstileProps = {
/** Called with the token once solved, or null on expiry/error. */
onToken: (token: null | string) => void
/**
* Change this value to make the widget issue a NEW token (a token is
* single-use: after a submission the server refused, the next one needs a
* fresh one). useTurnstile's `reset()` does it for the provider's widget.
*/
resetSignal?: number
/**
* Public Turnstile site key. The client project reads it server-side from
* SiteIntegrations (turnstileSiteKey) and passes it in — the widget is a pure
@@ -54,7 +60,7 @@ export type TurnstileTheme = 'auto' | 'dark' | 'light' | 'page'
* The script is injected directly (no next/script dependency) so the widget
* stays framework-agnostic.
*/
export function Turnstile({ onToken, siteKey, theme = 'page' }: TurnstileProps) {
export function Turnstile({ onToken, resetSignal = 0, siteKey, theme = 'page' }: TurnstileProps) {
const ref = useRef<HTMLDivElement>(null)
const widgetId = useRef<null | string>(null)
const pageTheme = useTheme().theme
@@ -95,6 +101,14 @@ export function Turnstile({ onToken, siteKey, theme = 'page' }: TurnstileProps)
return () => clearInterval(interval)
}, [siteKey, widgetTheme, onToken])
// reset(): run the challenge again — the old token was spent on the refused request.
const lastReset = useRef(resetSignal)
useEffect(() => {
if (lastReset.current === resetSignal) {return}
lastReset.current = resetSignal
if (widgetId.current) {window.turnstile?.reset(widgetId.current)}
}, [resetSignal])
if (!siteKey) {return null}
return <div ref={ref} />
+22 -8
View File
@@ -7,6 +7,10 @@ import type { TurnstileTheme } from './Turnstile.js'
import { Turnstile } from './Turnstile.js'
type TurnstileContextValue = {
/** Clears the token and makes the widget issue a new one. */
reset: () => void
/** Bumped by `reset` — the widget re-runs its challenge when it changes. */
resetSignal: number
setToken: (t: null | string) => void
/** Public site key from the provider (read server-side, passed once). */
siteKey: null | string
@@ -39,8 +43,13 @@ export function TurnstileProvider({
siteKey: null | string
}) {
const [token, setToken] = useState<null | string>(null)
const [resetSignal, setResetSignal] = useState(0)
const reset = useCallback(() => {
setToken(null)
setResetSignal((n) => n + 1)
}, [])
return (
<TurnstileContext value={{ setToken, siteKey, token }}>
<TurnstileContext value={{ reset, resetSignal, setToken, siteKey, token }}>
{children}
</TurnstileContext>
)
@@ -53,7 +62,7 @@ export function TurnstileProvider({
* const { token, TurnstileWidget, reset } = useTurnstile()
* // in JSX: <TurnstileWidget />
* // at submit: submitForm({ ..., turnstileToken: token })
* // after submit: reset() // clear for the next submission
* // after a refused submit: reset() // the token is spent — the widget issues a new one
*/
export function useTurnstile(): {
/** True when Turnstile is configured (site key present). */
@@ -69,7 +78,7 @@ export function useTurnstile(): {
return useTurnstileFrom(ctx)
}
const NO_PROVIDER: TurnstileContextValue = { setToken: () => {}, siteKey: null, token: null }
const NO_PROVIDER: TurnstileContextValue = { reset: () => {}, resetSignal: 0, setToken: () => {}, siteKey: null, token: null }
/**
* useTurnstile without the provider requirement: outside <TurnstileProvider>
@@ -81,17 +90,22 @@ export function useOptionalTurnstile(): ReturnType<typeof useTurnstile> {
}
function useTurnstileFrom(ctx: TurnstileContextValue): ReturnType<typeof useTurnstile> {
const { setToken, siteKey, token } = ctx
const reset = useCallback(() => setToken(null), [setToken])
const { reset, siteKey, token } = ctx
const TurnstileWidget = useCallback(
(props?: { theme?: TurnstileTheme }) => {
if (!siteKey) {return null}
return <Turnstile onToken={setToken} siteKey={siteKey} theme={props?.theme} />
return <ProvidedTurnstile theme={props?.theme} />
},
[siteKey, setToken],
[siteKey],
)
return { enabled: Boolean(siteKey), reset, token, TurnstileWidget }
}
/** The provider's widget: reads the key and the reset signal from the context, so `reset()` reaches it without a remount. */
function ProvidedTurnstile({ theme }: { theme?: TurnstileTheme }) {
const ctx = use(TurnstileContext)
if (!ctx?.siteKey) {return null}
return <Turnstile onToken={ctx.setToken} resetSignal={ctx.resetSignal} siteKey={ctx.siteKey} theme={theme} />
}