All files / apps/huella-legal/layers/forms/app/components FormChallenge.vue

88.88% Statements 16/18
100% Branches 3/3
60% Functions 3/5
94.11% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52  25x           4x                             9x   12x   12x 12x 12x   12x 6x     12x 5x   5x         4x   4x 4x   4x        
<template>
    <div @focusin.once="arm">
        <slot :challenge />
        <!-- The widget keeps a hidden input even while invisible, so only a showing checkbox gets a gap -->
        <NuxtTurnstile
            v-if="armed"
            ref="widget"
            v-model="token"
            :options="{
                appearance: 'interaction-only',
                'before-interactive-callback': () => (interactive = true),
                'after-interactive-callback': () => (interactive = false),
            }"
            :class="{ 'mt-3': interactive }"
        />
    </div>
</template>
 
<script setup lang="ts">
import type { Challenge } from "../types/Challenge";
import { until } from "@vueuse/core";
 
const TOKEN_TIMEOUT_MS = 30_000;
 
const widget = useTemplateRef("widget");
 
const armed = ref<boolean>(false);
const interactive = ref<boolean>(false);
const token = ref<string>();
 
const arm = (): void => {
    armed.value = true;
};
 
const challenge: Challenge = async (send) => {
    arm();
 
    const ready = await until(token).toBeTruthy({
        timeout: TOKEN_TIMEOUT_MS,
        throwOnTimeout: true,
    });
 
    token.value = undefined;
 
    try {
        return await send(ready);
    } finally {
        widget.value?.reset();
    }
};
</script>