UNPKG

react-google-recaptcha-v3

Version:
25 lines (22 loc) 12.8 kB
import e,{createContext as t,useState as r,useRef as o,useEffect as n,useCallback as a,useMemo as c,useContext as i}from"react"; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */var s=function(){return s=Object.assign||function(e){for(var t,r=1,o=arguments.length;r<o;r++)for(var n in t=arguments[r])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e},s.apply(this,arguments)};function u(e,t,r,o){return new(r||(r=Promise))((function(n,a){function c(e){try{s(o.next(e))}catch(e){a(e)}}function i(e){try{s(o.throw(e))}catch(e){a(e)}}function s(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(c,i)}s((o=o.apply(e,t||[])).next())}))}function l(e,t){var r,o,n,a,c={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return a={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function i(a){return function(i){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;c;)try{if(r=1,o&&(n=2&a[0]?o.return:a[0]?o.throw||((n=o.return)&&n.call(o),0):o.next)&&!(n=n.call(o,a[1])).done)return n;switch(o=0,n&&(a=[2&a[0],n.value]),a[0]){case 0:case 1:n=a;break;case 4:return c.label++,{value:a[1],done:!1};case 5:c.label++,o=a[1],a=[0];continue;case 7:a=c.ops.pop(),c.trys.pop();continue;default:if(!(n=c.trys,(n=n.length>0&&n[n.length-1])||6!==a[0]&&2!==a[0])){c=0;continue}if(3===a[0]&&(!n||a[1]>n[0]&&a[1]<n[3])){c.label=a[1];break}if(6===a[0]&&c.label<n[1]){c.label=n[1],n=a;break}if(n&&c.label<n[2]){c.label=n[2],c.ops.push(a);break}n[2]&&c.ops.pop(),c.trys.pop();continue}a=t.call(e,c)}catch(e){a=[6,e],o=0}finally{r=n=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,i])}}}var f,p=function(e){var t;e?function(e){if(e)for(;e.lastChild;)e.lastChild.remove()}("string"==typeof e?document.getElementById(e):e):(t=document.querySelector(".grecaptcha-badge"))&&t.parentNode&&document.body.removeChild(t.parentNode)},d=function(e,t){p(t),window.___grecaptcha_cfg=void 0;var r=document.querySelector("#"+e);r&&r.remove(),function(){var e=document.querySelector('script[src^="https://www.gstatic.com/recaptcha/releases"]');e&&e.remove()}()},y=function(e){var t=e.render,r=e.onLoadCallbackName,o=e.language,n=e.onLoad,a=e.useRecaptchaNet,c=e.useEnterprise,i=e.scriptProps,s=void 0===i?{}:i,u=s.nonce,l=void 0===u?"":u,f=s.defer,p=void 0!==f&&f,d=s.async,y=void 0!==d&&d,m=s.id,v=void 0===m?"":m,b=s.appendTo,h=v||"google-recaptcha-v3";if(function(e){return!!document.querySelector("#"+e)}(h))n();else{var g=function(e){return"https://www."+(e.useRecaptchaNet?"recaptcha.net":"google.com")+"/recaptcha/"+(e.useEnterprise?"enterprise.js":"api.js")}({useEnterprise:c,useRecaptchaNet:a}),S=document.createElement("script");S.id=h,S.src=g+"?render="+t+("explicit"===t?"&onload="+r:"")+(o?"&hl="+o:""),l&&(S.nonce=l),S.defer=!!p,S.async=!!y,S.onload=n,("body"===b?document.body:document.getElementsByTagName("head")[0]).appendChild(S)}},m=function(e){"undefined"!=typeof process&&!!process.env&&"production"!==process.env.NODE_ENV||console.warn(e)};!function(e){e.SCRIPT_NOT_AVAILABLE="Recaptcha script is not available"}(f||(f={}));var v=t({executeRecaptcha:function(){throw Error("GoogleReCaptcha Context has not yet been implemented, if you are using useGoogleReCaptcha hook, make sure the hook is called inside component wrapped by GoogleRecaptchaProvider")}}),b=v.Consumer;function h(t){var i=t.reCaptchaKey,u=t.useEnterprise,l=void 0!==u&&u,p=t.useRecaptchaNet,b=void 0!==p&&p,h=t.scriptProps,g=t.language,S=t.container,w=t.children,$=r(null),C=$[0],P=$[1],x=o(i),E=JSON.stringify(h),R=JSON.stringify(null==S?void 0:S.parameters);n((function(){if(i){var e=(null==h?void 0:h.id)||"google-recaptcha-v3",t=(null==h?void 0:h.onLoadCallbackName)||"onRecaptchaLoadCallback";window[t]=function(){var e=l?window.grecaptcha.enterprise:window.grecaptcha,t=s({badge:"inline",size:"invisible",sitekey:i},(null==S?void 0:S.parameters)||{});x.current=e.render(null==S?void 0:S.element,t)};return y({render:(null==S?void 0:S.element)?"explicit":i,onLoadCallbackName:t,useEnterprise:l,useRecaptchaNet:b,scriptProps:h,language:g,onLoad:function(){if(window&&window.grecaptcha){var e=l?window.grecaptcha.enterprise:window.grecaptcha;e.ready((function(){P(e)}))}else m("<GoogleRecaptchaProvider /> "+f.SCRIPT_NOT_AVAILABLE)},onError:function(){m("Error loading google recaptcha script")}}),function(){d(e,null==S?void 0:S.element)}}m("<GoogleReCaptchaProvider /> recaptcha key not provided")}),[l,b,E,R,g,i,null==S?void 0:S.element]);var M=a((function(e){if(!C||!C.execute)throw new Error("<GoogleReCaptchaProvider /> Google Recaptcha has not been loaded");return C.execute(x.current,{action:e})}),[C,x]),N=c((function(){return{executeRecaptcha:C?M:void 0,container:null==S?void 0:S.element}}),[M,C,null==S?void 0:S.element]);return e.createElement(v.Provider,{value:N},w)}var g=function(){return i(v)};function S(t){var r=this,o=t.action,a=t.onVerify,c=t.refreshReCaptcha,i=g();n((function(){var e=i.executeRecaptcha;if(e){u(r,void 0,void 0,(function(){var t;return l(this,(function(r){switch(r.label){case 0:return[4,e(o)];case 1:return t=r.sent(),a?(a(t),[2]):(m("Please define an onVerify function"),[2])}}))}))}}),[o,a,c,i]);var s=i.container;return"string"==typeof s?e.createElement("div",{id:s}):null}function w(e,t){return e(t={exports:{}},t.exports),t.exports /** @license React v16.13.1 * react-is.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */}var $="function"==typeof Symbol&&Symbol.for,C=$?Symbol.for("react.element"):60103,P=$?Symbol.for("react.portal"):60106,x=$?Symbol.for("react.fragment"):60107,E=$?Symbol.for("react.strict_mode"):60108,R=$?Symbol.for("react.profiler"):60114,M=$?Symbol.for("react.provider"):60109,N=$?Symbol.for("react.context"):60110,O=$?Symbol.for("react.async_mode"):60111,_=$?Symbol.for("react.concurrent_mode"):60111,T=$?Symbol.for("react.forward_ref"):60112,j=$?Symbol.for("react.suspense"):60113,L=$?Symbol.for("react.suspense_list"):60120,k=$?Symbol.for("react.memo"):60115,F=$?Symbol.for("react.lazy"):60116,A=$?Symbol.for("react.block"):60121,V=$?Symbol.for("react.fundamental"):60117,z=$?Symbol.for("react.responder"):60118,G=$?Symbol.for("react.scope"):60119;function I(e){if("object"==typeof e&&null!==e){var t=e.$$typeof;switch(t){case C:switch(e=e.type){case O:case _:case x:case R:case E:case j:return e;default:switch(e=e&&e.$$typeof){case N:case T:case F:case k:case M:return e;default:return t}}case P:return t}}}function D(e){return I(e)===_}var q={AsyncMode:O,ConcurrentMode:_,ContextConsumer:N,ContextProvider:M,Element:C,ForwardRef:T,Fragment:x,Lazy:F,Memo:k,Portal:P,Profiler:R,StrictMode:E,Suspense:j,isAsyncMode:function(e){return D(e)||I(e)===O},isConcurrentMode:D,isContextConsumer:function(e){return I(e)===N},isContextProvider:function(e){return I(e)===M},isElement:function(e){return"object"==typeof e&&null!==e&&e.$$typeof===C},isForwardRef:function(e){return I(e)===T},isFragment:function(e){return I(e)===x},isLazy:function(e){return I(e)===F},isMemo:function(e){return I(e)===k},isPortal:function(e){return I(e)===P},isProfiler:function(e){return I(e)===R},isStrictMode:function(e){return I(e)===E},isSuspense:function(e){return I(e)===j},isValidElementType:function(e){return"string"==typeof e||"function"==typeof e||e===x||e===_||e===R||e===E||e===j||e===L||"object"==typeof e&&null!==e&&(e.$$typeof===F||e.$$typeof===k||e.$$typeof===M||e.$$typeof===N||e.$$typeof===T||e.$$typeof===V||e.$$typeof===z||e.$$typeof===G||e.$$typeof===A)},typeOf:I},B=w((function(e,t){"production"!==process.env.NODE_ENV&&function(){var e="function"==typeof Symbol&&Symbol.for,r=e?Symbol.for("react.element"):60103,o=e?Symbol.for("react.portal"):60106,n=e?Symbol.for("react.fragment"):60107,a=e?Symbol.for("react.strict_mode"):60108,c=e?Symbol.for("react.profiler"):60114,i=e?Symbol.for("react.provider"):60109,s=e?Symbol.for("react.context"):60110,u=e?Symbol.for("react.async_mode"):60111,l=e?Symbol.for("react.concurrent_mode"):60111,f=e?Symbol.for("react.forward_ref"):60112,p=e?Symbol.for("react.suspense"):60113,d=e?Symbol.for("react.suspense_list"):60120,y=e?Symbol.for("react.memo"):60115,m=e?Symbol.for("react.lazy"):60116,v=e?Symbol.for("react.block"):60121,b=e?Symbol.for("react.fundamental"):60117,h=e?Symbol.for("react.responder"):60118,g=e?Symbol.for("react.scope"):60119;function S(e){if("object"==typeof e&&null!==e){var t=e.$$typeof;switch(t){case r:var d=e.type;switch(d){case u:case l:case n:case c:case a:case p:return d;default:var v=d&&d.$$typeof;switch(v){case s:case f:case m:case y:case i:return v;default:return t}}case o:return t}}}var w=u,$=l,C=s,P=i,x=r,E=f,R=n,M=m,N=y,O=o,_=c,T=a,j=p,L=!1;function k(e){return S(e)===l}t.AsyncMode=w,t.ConcurrentMode=$,t.ContextConsumer=C,t.ContextProvider=P,t.Element=x,t.ForwardRef=E,t.Fragment=R,t.Lazy=M,t.Memo=N,t.Portal=O,t.Profiler=_,t.StrictMode=T,t.Suspense=j,t.isAsyncMode=function(e){return L||(L=!0,console.warn("The ReactIs.isAsyncMode() alias has been deprecated, and will be removed in React 17+. Update your code to use ReactIs.isConcurrentMode() instead. It has the exact same API.")),k(e)||S(e)===u},t.isConcurrentMode=k,t.isContextConsumer=function(e){return S(e)===s},t.isContextProvider=function(e){return S(e)===i},t.isElement=function(e){return"object"==typeof e&&null!==e&&e.$$typeof===r},t.isForwardRef=function(e){return S(e)===f},t.isFragment=function(e){return S(e)===n},t.isLazy=function(e){return S(e)===m},t.isMemo=function(e){return S(e)===y},t.isPortal=function(e){return S(e)===o},t.isProfiler=function(e){return S(e)===c},t.isStrictMode=function(e){return S(e)===a},t.isSuspense=function(e){return S(e)===p},t.isValidElementType=function(e){return"string"==typeof e||"function"==typeof e||e===n||e===l||e===c||e===a||e===p||e===d||"object"==typeof e&&null!==e&&(e.$$typeof===m||e.$$typeof===y||e.$$typeof===i||e.$$typeof===s||e.$$typeof===f||e.$$typeof===b||e.$$typeof===h||e.$$typeof===g||e.$$typeof===v)},t.typeOf=S}()})),J=(B.AsyncMode,B.ConcurrentMode,B.ContextConsumer,B.ContextProvider,B.Element,B.ForwardRef,B.Fragment,B.Lazy,B.Memo,B.Portal,B.Profiler,B.StrictMode,B.Suspense,B.isAsyncMode,B.isConcurrentMode,B.isContextConsumer,B.isContextProvider,B.isElement,B.isForwardRef,B.isFragment,B.isLazy,B.isMemo,B.isPortal,B.isProfiler,B.isStrictMode,B.isSuspense,B.isValidElementType,B.typeOf,w((function(e){"production"===process.env.NODE_ENV?e.exports=q:e.exports=B}))),K={childContextTypes:!0,contextType:!0,contextTypes:!0,defaultProps:!0,displayName:!0,getDefaultProps:!0,getDerivedStateFromError:!0,getDerivedStateFromProps:!0,mixins:!0,propTypes:!0,type:!0},U={name:!0,length:!0,prototype:!0,caller:!0,callee:!0,arguments:!0,arity:!0},H={$$typeof:!0,compare:!0,defaultProps:!0,displayName:!0,propTypes:!0,type:!0},Q={};function W(e){return J.isMemo(e)?H:Q[e.$$typeof]||K}Q[J.ForwardRef]={$$typeof:!0,render:!0,defaultProps:!0,displayName:!0,propTypes:!0},Q[J.Memo]=H;var X=Object.defineProperty,Y=Object.getOwnPropertyNames,Z=Object.getOwnPropertySymbols,ee=Object.getOwnPropertyDescriptor,te=Object.getPrototypeOf,re=Object.prototype;var oe=function e(t,r,o){if("string"!=typeof r){if(re){var n=te(r);n&&n!==re&&e(t,n,o)}var a=Y(r);Z&&(a=a.concat(Z(r)));for(var c=W(t),i=W(r),s=0;s<a.length;++s){var u=a[s];if(!(U[u]||o&&o[u]||i&&i[u]||c&&c[u])){var l=ee(r,u);try{X(t,u,l)}catch(e){}}}}return t},ne=function(t){var r=function(r){return e.createElement(b,null,(function(o){return e.createElement(t,s({},r,{googleReCaptchaProps:o}))}))};return r.displayName="withGoogleReCaptcha("+(t.displayName||t.name||"Component")+")",oe(r,t),r};export{S as GoogleReCaptcha,b as GoogleReCaptchaConsumer,v as GoogleReCaptchaContext,h as GoogleReCaptchaProvider,g as useGoogleReCaptcha,ne as withGoogleReCaptcha}; //# sourceMappingURL=react-google-recaptcha-v3.esm.js.map