Vue reCAPTCHA v3 Integration

2.0.1 · active · verified Sun Apr 19

Vue reCAPTCHA-v3 is a library designed to simplify the integration of Google reCAPTCHA v3 into Vue.js applications. It leverages the underlying `recaptcha-v3` package to provide an easy-to-use API for executing reCAPTCHA actions and obtaining tokens. The current stable version, 2.0.1, fully supports Vue 3, with previous versions (specifically ^1.9.0) available for Vue 2 applications. The package has a moderate release cadence, with updates typically coinciding with feature additions, bug fixes, or updates to the core `recaptcha-v3` dependency. Its key differentiator is its straightforward API for both Vue 2 (options API) and Vue 3 (composition API), abstracting away the complexities of the reCAPTCHA JavaScript API, and offering optional loader configurations for advanced use cases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `vue-recaptcha-v3` plugin for Vue 3 using the Composition API, execute a reCAPTCHA action, and retrieve the token. It includes an optional environment variable for the site key and shows basic console logging of the token.

import { createApp } from 'vue'
import { VueReCaptcha, useReCaptcha } from 'vue-recaptcha-v3'

const VUE_APP_RECAPTCHA_SITE_KEY = process.env.VUE_APP_RECAPTCHA_SITE_KEY ?? 'YOUR_SITE_KEY_HERE';

const App = {
  setup() {
    const { executeRecaptcha, recaptchaLoaded } = useReCaptcha()

    const handleRecaptcha = async () => {
      console.log('Executing reCAPTCHA...')
      // (optional) Wait until recaptcha has been loaded.
      await recaptchaLoaded()

      // Execute reCAPTCHA with a specific action.
      const token = await executeRecaptcha('submit_form')

      console.log('reCAPTCHA token:', token)
      // In a real application, you would send this token to your backend for verification.
      alert(`reCAPTCHA token: ${token}\n(Check console for more details)`) 
    }

    return {
      handleRecaptcha
    }
  },
  template: `
    <div>
      <h1>Vue reCAPTCHA v3 Quickstart</h1>
      <p>Click the button below to execute a reCAPTCHA v3 action and get a token.</p>
      <button @click="handleRecaptcha">Execute reCAPTCHA</button>
      <p>Note: The reCAPTCHA badge might be hidden if 'useRecaptchaNet' is enabled or via instance methods.</p>
    </div>
  `
}

createApp(App)
  .use(VueReCaptcha, { siteKey: VUE_APP_RECAPTCHA_SITE_KEY, loaderOptions: { useRecaptchaNet: true }})
  .mount('#app')

view raw JSON →