Vue Cloudflare Turnstile Component

1.0.11 · active · verified Tue Apr 21

vue-turnstile is a Vue 3 component library that simplifies the integration of Cloudflare Turnstile, a CAPTCHA alternative, into Vue applications. It provides a declarative `VueTurnstile` component that handles the underlying Turnstile API interactions, script loading, and token management. The current stable version is 1.0.11, suggesting a mature library actively maintained for Vue 3 environments. While a specific release cadence isn't published, the `1.0.x` versioning implies ongoing maintenance and minor updates as needed. Key differentiators include its tight integration with Vue's reactivity system (e.g., `v-model` for token binding), support for various Turnstile customization options like theme, size, and language, and explicit methods for resetting and re-rendering the widget. It abstracts away the complexities of directly interacting with the Cloudflare Turnstile JavaScript API, making it easier for developers to implement bot protection.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate the `VueTurnstile` component into a Vue 3 application, bind the generated token using `v-model`, and display the token. It also shows how to access component methods like `reset()` via a template ref.

<script lang="ts">
import VueTurnstile from 'vue-turnstile';

export default {
  components: { VueTurnstile },

  data() {
    return {
      token: '',
    };
  },
  methods: {
    // Example of how to access component methods via ref
    resetTurnstile() {
      (this.$refs.turnstileWidget as typeof VueTurnstile & { reset: () => void } | undefined)?.reset();
    }
  }
};
</script>

<template>
  <div>
    <p>Please complete the CAPTCHA:</p>
    <vue-turnstile
      site-key="1x00000000000000000000AA" <!-- Replace with your actual site key -->
      v-model="token"
      theme="auto"
      size="normal"
      language="en"
      ref="turnstileWidget"
    />
    <div v-if="token">
      <p>Turnstile Token: <code>{{ token }}</code></p>
      <button @click="resetTurnstile">Reset Turnstile</button>
    </div>
    <div v-else>
      <p>Awaiting Turnstile token...</p>
    </div>
  </div>
</template>

<style>
/* Basic styling for demonstration */
div {
  font-family: Arial, sans-serif;
  margin-bottom: 10px;
}
button {
  padding: 8px 15px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
button:hover {
  background-color: #0056b3;
}
</style>

view raw JSON →