Line data Source code
1 : import { computed, ref, watch } from 'vue'
2 : import type { Ref } from 'vue'
3 :
4 : /**
5 : * Submit-gating state for a signup form's Turnstile widget: a token/
6 : * unavailable pair, plus `waiting`, which is true while a real token is still
7 : * needed. Waits in both shadow and enforce mode (`enabled`), not just
8 : * enforce, so shadow mode's token can't race the async Cloudflare
9 : * challenge; falls back open once the widget reports `unavailable` so a
10 : * broken/slow load can never permanently block signup.
11 : *
12 : * `token`/`unavailable` reset on every `enabled` transition, in either
13 : * direction, so state from a previous widget instance can never leak into a
14 : * freshly (re-)rendered one.
15 : */
16 2 : export function useTurnstileGate(enabled: Ref<boolean>) {
17 2 : const token = ref('')
18 2 : const unavailable = ref(false)
19 :
20 2 : const waiting = computed(
21 : () => enabled.value && !token.value && !unavailable.value
22 : )
23 :
24 2 : watch(enabled, () => {
25 0 : token.value = ''
26 0 : unavailable.value = false
27 : })
28 :
29 2 : return { token, unavailable, waiting }
30 : }
|