Vue reCAPTCHA v3 Integration
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
-
TypeError: app.component is not a function
cause Attempting to use `vue-recaptcha-v3` v2.x (Vue 3 compatible) with a Vue 2 application setup (e.g., `import Vue from 'vue'; Vue.use(...)`).fixFor Vue 2 applications, downgrade `vue-recaptcha-v3` to a `^1.9.0` version. For Vue 3, ensure your application setup uses `createApp()` and the correct import for `VueReCaptcha`. -
Uncaught (in promise) Error: reCAPTCHA v3 site key not found
cause The `siteKey` option was either not provided or was an empty string during the plugin installation.fixEnsure you pass a valid Google reCAPTCHA v3 site key to the `Vue.use(VueReCaptcha, { siteKey: 'YOUR_SITE_KEY' })` configuration object. -
Property '$recaptcha' does not exist on type 'ComponentPublicInstance<...>'
cause Using Vue 3 Options API with TypeScript without extending the Vue instance's type definitions for `$recaptcha` and related properties.fixCreate a `shims-vue-recaptcha-v3.d.ts` file in your project with the provided type declarations from the package documentation to augment `ComponentCustomProperties`.
Warnings
- breaking Version 2.0.0 of `vue-recaptcha-v3` introduces full support for Vue 3 and is not backward compatible with Vue 2 applications. Attempting to use v2.x with Vue 2 will result in runtime errors.
- gotcha reCAPTCHA v3 requires a valid site key obtained from the Google reCAPTCHA admin console for your specific domain. Using an invalid or incorrect key will prevent reCAPTCHA from loading or executing correctly, leading to failed token generation.
- gotcha When using TypeScript with the Vue 3 Options API, instance properties like `$recaptcha`, `$recaptchaLoaded`, and `$recaptchaInstance` will not have type suggestions or checks by default.
- gotcha The `useReCaptcha` composition function provides `executeRecaptcha` and `recaptchaLoaded`. Earlier versions of the documentation (and potentially common user typos) might refer to `useRecaptcha` or `executeRecaptcha` as `executeRecaptcha` (lowercase 'c').
Install
-
npm install vue-recaptcha-v3 -
yarn add vue-recaptcha-v3 -
pnpm add vue-recaptcha-v3
Imports
- VueReCaptcha
const { VueReCaptcha } = require('vue-recaptcha-v3')import { VueReCaptcha } from 'vue-recaptcha-v3' - useReCaptcha
import { useRecaptcha } from 'vue-recaptcha-v3'import { useReCaptcha } from 'vue-recaptcha-v3' - ComponentCustomProperties
declare module 'vue' { interface Vue { $recaptcha: (action: string) => Promise<string> } }declare module '@vue/runtime-core' { interface ComponentCustomProperties { $recaptcha: (action: string) => Promise<string> } }
Quickstart
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')