UNPKG

@anilkumarthakur/vue3-recaptcha

Version:

This plugin provides a Vue 3 component and plugin for integrating Google reCAPTCHA V3. The plugin enables easy reCAPTCHA token generation, management, and configuration within a Vue 3 application.

61 lines (60 loc) 1.76 kB
import { defineComponent as l, computed as m, inject as w, createElementBlock as y, openBlock as f } from "vue"; const g = /* @__PURE__ */ l({ __name: "RecaptchaV3", props: { siteKey: {}, action: {}, modelValue: {} }, emits: ["update:modelValue"], setup(n, { expose: o, emit: d }) { const i = n, s = m({ get: () => i.modelValue, set: (e) => u("update:modelValue", e || "") }), u = d; function r() { return new Promise((e, c) => { window.grecaptcha ? window.grecaptcha.ready(() => { p().then(e).catch(c); }) : c(new Error("Recaptcha is not loaded")); }); } async function p() { return window.grecaptcha.execute(i.siteKey ?? (t == null ? void 0 : t.siteKey) ?? "", { action: i.action ?? (t == null ? void 0 : t.action) ?? "" }).then((e) => (s.value = e, e)); } async function h() { return new Promise((e, c) => { if (window.grecaptcha) r().then(e).catch(c); else { const a = document.createElement("script"); a.src = `https://www.google.com/recaptcha/api.js?render=${i.siteKey || (t ? t.siteKey : "")}`, a.async = !0, a.defer = !0, a.onload = () => { r().then(e).catch(c); }, document.head.appendChild(a); } }); } o({ loadRecaptcha: h, executeRecaptcha: p, initializeRecaptcha: r, recaptchaToken: s }); const t = w("recaptcha"); return (e, c) => (f(), y("div")); } }), R = { install(n, o) { n.component("RecaptchaV3", g), n.provide("recaptcha", { siteKey: o.siteKey, action: o.action }); } }; export { g as RecaptchaV3, R as recaptchaPlugin }; //# sourceMappingURL=index.es.js.map