UNPKG

google-react-recaptcha-v2

Version:
148 lines (141 loc) 4.91 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // libs/index.ts var index_exports = {}; __export(index_exports, { ReCaptchaV2: () => ReCaptchaV2_default }); module.exports = __toCommonJS(index_exports); // libs/presentation/ReCaptchaV2.tsx var import_react2 = require("react"); // libs/application/reCaptchaV2.usecase.ts var import_react = require("react"); // libs/infrastructure/reCaptchaV2.service.ts var ReCaptchaV2Service = class { scriptId = "recaptcha-script"; scriptSrc(hl) { return `https://www.google.com/recaptcha/api.js?render=explicit${hl ? `&hl=${hl}` : ""}`; } loadScript(hl) { if (!document.getElementById(this.scriptId)) { const script = document.createElement("script"); script.id = this.scriptId; script.src = this.scriptSrc(hl); script.async = true; script.defer = true; document.body.appendChild(script); } } renderRecaptcha(container, props, callbacks) { if (typeof grecaptcha !== "undefined" && container) { return grecaptcha.render(container, { sitekey: props.isDevelop ? "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI" : props.siteKey, size: props.size, badge: props.badge, theme: props.theme, tabindex: props.tabindex, callback: callbacks.onSuccess, "error-callback": callbacks.onError, "expired-callback": callbacks.onExpired }); } return null; } }; // libs/application/reCaptchaV2.usecase.ts function useReCaptchaV2(props, ref) { const recaptchaRef = (0, import_react.useRef)(null); const widgetIdRef = (0, import_react.useRef)(null); const service = new ReCaptchaV2Service(); (0, import_react.useImperativeHandle)(ref, () => ({ execute: () => { return new Promise((resolve, reject) => { if (typeof grecaptcha !== "undefined" && widgetIdRef.current !== null) { const originalCallback = props.onSuccess; const tempCallback = (token) => { resolve(token); if (originalCallback) originalCallback(token); grecaptcha.reset(widgetIdRef.current ?? void 0); }; window.___tempRecaptchaCallback = tempCallback; grecaptcha.execute(widgetIdRef.current.toString()); } else { reject("grecaptcha no disponible"); } }); }, reset: () => { if (typeof grecaptcha !== "undefined" && widgetIdRef.current !== null) { grecaptcha.reset(widgetIdRef.current); } }, getResponse: () => { if (typeof grecaptcha !== "undefined" && widgetIdRef.current !== null) { return grecaptcha.getResponse(widgetIdRef.current); } return null; } })); (0, import_react.useEffect)(() => { service.loadScript(props.hl); const render = () => { if (typeof grecaptcha !== "undefined" && recaptchaRef.current && widgetIdRef.current === null) { widgetIdRef.current = service.renderRecaptcha( recaptchaRef.current, props, { onSuccess: (token) => { if (window.___tempRecaptchaCallback) { window.___tempRecaptchaCallback(token); delete window.___tempRecaptchaCallback; } else if (props.onSuccess) { props.onSuccess(token); } }, onError: props.onError, onExpired: props.onExpired } ); } }; const intervalId = setInterval(() => { if (typeof grecaptcha !== "undefined") { clearInterval(intervalId); render(); } }, 500); return () => { clearInterval(intervalId); }; }, [props]); return recaptchaRef; } // libs/presentation/ReCaptchaV2.tsx var import_jsx_runtime = require("react/jsx-runtime"); var ReCaptchaV2 = (0, import_react2.forwardRef)( (props, ref) => { const recaptchaRef = useReCaptchaV2(props, ref); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ref: recaptchaRef }); } ); var ReCaptchaV2_default = ReCaptchaV2; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ReCaptchaV2 });