react-native-recaptcha-that-works
Version:
⚛ A reCAPTCHA bridge for React Native that works.
178 lines • 7.42 kB
JavaScript
"use strict";
/*
* MIT License
*
* Copyright (c) 2020 Douglas Nassif Roma Junior
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_native_1 = require("react-native");
const react_native_webview_1 = __importDefault(require("react-native-webview"));
const get_template_1 = __importDefault(require("./get-template"));
const utils_1 = require("./utils");
const styles = react_native_1.StyleSheet.create({
webView: {
flex: 1,
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
loadingContainer: {
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
right: 0,
justifyContent: 'center',
alignItems: 'center',
},
});
const originWhitelist = ['*'];
const Recaptcha = (0, react_1.forwardRef)(({ headerComponent, footerComponent, loadingComponent, webViewProps, modalProps, onVerify, onExpire, onError, onClose, onLoad, theme = 'light', size = 'normal', siteKey, baseUrl, lang, style, enterprise = false, recaptchaDomain = 'www.google.com', gstaticDomain = 'www.gstatic.com', hideBadge = false, action, }, $ref) => {
const $isClosed = (0, react_1.useRef)(true);
const $webView = (0, react_1.useRef)(null);
const [visible, setVisible] = (0, react_1.useState)(false);
const [loading, setLoading] = (0, react_1.useState)(true);
const isInvisibleSize = size === 'invisible';
const html = (0, react_1.useMemo)(() => {
return (0, get_template_1.default)({
siteKey,
size,
theme,
lang,
action,
}, recaptchaDomain, gstaticDomain, enterprise, hideBadge);
}, [
siteKey,
size,
theme,
lang,
action,
enterprise,
recaptchaDomain,
gstaticDomain,
hideBadge,
]);
const handleLoad = (0, react_1.useCallback)(() => {
onLoad?.();
if (isInvisibleSize) {
$webView.current?.injectJavaScript(`
window.rnRecaptcha.execute();
`);
}
setLoading(false);
}, [onLoad, isInvisibleSize]);
const handleClose = (0, react_1.useCallback)(() => {
if ($isClosed.current) {
return;
}
$isClosed.current = true;
setVisible(false);
onClose?.();
}, [onClose]);
const handleMessage = (0, react_1.useCallback)((event) => {
try {
const payload = JSON.parse(event.nativeEvent.data);
if ((0, utils_1.isPayloadClose)(payload) && isInvisibleSize) {
handleClose();
}
if ((0, utils_1.isPayloadLoad)(payload)) {
handleLoad();
}
if ((0, utils_1.isPayloadExpire)(payload)) {
onExpire?.();
}
if ((0, utils_1.isPayloadError)(payload)) {
handleClose();
onError?.(...payload.error);
}
if ((0, utils_1.isPayloadVerify)(payload)) {
handleClose();
onVerify?.(...payload.verify);
}
}
catch (err) {
if ('__DEV__' in global && __DEV__) {
console.warn(err);
}
}
}, [onVerify, onExpire, onError, handleClose, handleLoad, isInvisibleSize]);
const source = (0, react_1.useMemo)(() => ({
html,
baseUrl,
}), [html, baseUrl]);
(0, react_1.useImperativeHandle)($ref, () => ({
open: () => {
setVisible(true);
setLoading(true);
$isClosed.current = false;
},
close: handleClose,
}), [handleClose]);
const handleNavigationStateChange = (0, react_1.useCallback)(() => {
// prevent navigation on Android
if (!loading) {
$webView.current?.stopLoading();
}
}, [loading]);
const handleShouldStartLoadWithRequest = (0, react_1.useCallback)(event => {
// prevent navigation on iOS
return event.navigationType === 'other';
}, []);
const webViewStyles = (0, react_1.useMemo)(() => [styles.webView, style], [style]);
const renderLoading = () => {
if (!loading && source) {
return null;
}
return (react_1.default.createElement(react_native_1.View, { style: styles.loadingContainer }, loadingComponent || react_1.default.createElement(react_native_1.ActivityIndicator, { size: "large" })));
};
return (react_1.default.createElement(react_native_1.Modal, { transparent: true, ...modalProps, visible: visible, onRequestClose: handleClose },
headerComponent,
react_1.default.createElement(react_native_webview_1.default, { bounces: false, allowsBackForwardNavigationGestures: false, originWhitelist: originWhitelist, onShouldStartLoadWithRequest: handleShouldStartLoadWithRequest, onNavigationStateChange: handleNavigationStateChange, ...webViewProps, source: source, style: webViewStyles, onMessage: handleMessage, ref: $webView }),
footerComponent,
renderLoading()));
});
exports.default = Recaptcha;
//# sourceMappingURL=Recaptcha.js.map