Vue reCAPTCHA Component

3.0.0-alpha.6 · active · verified Sun Apr 19

vue-recaptcha is a Vue component designed for straightforward integration of Google reCAPTCHA v2 (checkbox) and v3 (invisible) into Vue applications. The current stable version is v2.0.3, which primarily supports Vue 3, but also offers Vue 2 compatibility via `@vue/composition-api`. While major version releases (e.g., v1 to v2) have historically had slow cadences, minor bug fixes within a major version are released as needed. A v3.0.0-alpha.6 release is available, indicating future developments. Key differentiators include encapsulating the reCAPTCHA script loading and widget rendering within a declarative Vue component, automatically handling script loading by default since v2.0.0, and shipping with TypeScript definitions for improved developer experience. It simplifies the process of getting a reCAPTCHA token and handling verification events.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Vue 3 application using the VueRecaptcha component. It shows how to import and register the component, pass a site key, automatically load the reCAPTCHA script, and handle verification, expiration, and error events to display the reCAPTCHA token or messages.

import { createApp } from 'vue';
import { VueRecaptcha } from 'vue-recaptcha';

const app = createApp({
  data() {
    return {
      sitekey: process.env.VUE_APP_RECAPTCHA_SITE_KEY ?? '',
      token: '',
      error: ''
    };
  },
  methods: {
    onVerify(response) {
      this.token = response;
      this.error = '';
      console.log('reCAPTCHA token:', response);
      // Send token to your backend for verification
    },
    onExpired() {
      this.token = '';
      console.log('reCAPTCHA token expired.');
    },
    onError(error) {
      this.error = error;
      this.token = '';
      console.error('reCAPTCHA error:', error);
    }
  },
  template: `
    <div>
      <h1>reCAPTCHA Example</h1>
      <VueRecaptcha
        :sitekey="sitekey"
        :loadRecaptchaScript="true"
        @verify="onVerify"
        @expired="onExpired"
        @error="onError"
      ></VueRecaptcha>
      <p v-if="token">Token: {{ token }}</p>
      <p v-if="error" style="color: red;">Error: {{ error }}</p>
      <p v-else-if="!token && sitekey">Waiting for reCAPTCHA verification...</p>
      <p v-else>Please set VUE_APP_RECAPTCHA_SITE_KEY environment variable.</p>
    </div>
  `
});

app.component('VueRecaptcha', VueRecaptcha);
app.mount('#app');

view raw JSON →