mui-extended
Version:
Extended UI Components built on Material UI
52 lines (51 loc) • 2.28 kB
JavaScript
import { __extends } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { Component, createContext, useContext } from "react";
var ReCaptchaContext = createContext({});
export var useReCaptchaContext = function () {
var reCaptchaContext = useContext(ReCaptchaContext);
if (!reCaptchaContext.getToken) {
throw new Error("useReCaptchaToken must be used with in ReCaptchaProvider");
}
return reCaptchaContext;
};
var ReCaptchaProvider = /** @class */ (function (_super) {
__extends(ReCaptchaProvider, _super);
function ReCaptchaProvider(props) {
var _this = _super.call(this, props) || this;
_this.getReCaptchaToken = _this.getReCaptchaToken.bind(_this);
return _this;
}
ReCaptchaProvider.prototype.componentDidMount = function () {
var head = document.getElementsByTagName("head")[0];
if (head.querySelector("script#recaptcha" + this.props.siteKey) == null) {
var scriptTag = document.createElement("script");
scriptTag.setAttribute("src", "https://www.google.com/recaptcha/enterprise.js?render=" +
this.props.siteKey);
scriptTag.id = "recaptcha" + this.props.siteKey;
head.appendChild(scriptTag);
var styleTag = document.createElement("style");
styleTag.innerText = ".grecaptcha-badge { display: none; }";
head.appendChild(styleTag);
}
};
ReCaptchaProvider.prototype.getReCaptchaToken = function (action) {
var _this = this;
return new Promise(function (resolve, reject) {
window.grecaptcha.enterprise.ready(function () {
window.grecaptcha.enterprise
.execute(_this.props.siteKey, { action: action })
.then(resolve)
.catch(reject);
});
setTimeout(function () {
reject(new Error("getReCaptchaToken timedout"));
}, 5000);
});
};
ReCaptchaProvider.prototype.render = function () {
return (_jsx(ReCaptchaContext.Provider, { value: { getToken: this.getReCaptchaToken }, children: this.props.children }));
};
return ReCaptchaProvider;
}(Component));
export { ReCaptchaProvider };