Skip to content

Commit

Permalink
Merge pull request #7 from johnlim5847/master
Browse files Browse the repository at this point in the history
add action props
  • Loading branch information
jeremy-farnault committed Jun 19, 2020
2 parents 0003adf + f4e3b87 commit a5830df
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export declare type IProps = {
captchaDomain: string;
onReceiveToken: (captchaToken: string) => void;
siteKey: string;
action: string;
};
export declare type IState = {};
declare class ReCaptchaV3 extends React.PureComponent<IProps, IState> {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ReCaptchaV3 extends React.PureComponent {
};
}
render() {
return (React.createElement(ReCaptchaComponent_1.default, { ref: ref => this._captchaRef = ref, captchaDomain: this.props.captchaDomain, siteKey: this.props.siteKey, onReceiveToken: (token) => {
return (React.createElement(ReCaptchaComponent_1.default, { ref: ref => this._captchaRef = ref, action: this.props.action, captchaDomain: this.props.captchaDomain, siteKey: this.props.siteKey, onReceiveToken: (token) => {
this.props.onReceiveToken(token);
} }));
}
Expand Down
1 change: 1 addition & 0 deletions dist/src/ReCaptchaComponent.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare type IProps = {
captchaDomain: string;
onReceiveToken: (captchaToken: string) => void;
siteKey: string;
action: string;
};
declare class ReCaptchaComponent extends React.PureComponent<IProps> {
private _webViewRef;
Expand Down
12 changes: 6 additions & 6 deletions dist/src/ReCaptchaComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ const patchPostMessageJsCode = `(${String(function () {
patchedPostMessage.toString = () => String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage');
window.postMessage = patchedPostMessage;
})})();`;
const getExecutionFunction = (siteKey) => {
return `window.grecaptcha.execute('${siteKey}', { action: 'login' }).then(
const getExecutionFunction = (siteKey, action) => {
return `window.grecaptcha.execute('${siteKey}', { action: '${action}' }).then(
function(args) {
window.ReactNativeWebView.postMessage(args);
}
)`;
};
const getInvisibleRecaptchaContent = (siteKey) => {
const getInvisibleRecaptchaContent = (siteKey, action) => {
return `<!DOCTYPE html><html><head>
<script src="https://www.google.com/recaptcha/api.js?render=${siteKey}"></script>
<script>window.grecaptcha.ready(function() { ${getExecutionFunction(siteKey)} });</script>
<script>window.grecaptcha.ready(function() { ${getExecutionFunction(siteKey, action)} });</script>
</head></html>`;
};
class ReCaptchaComponent extends React.PureComponent {
Expand All @@ -32,7 +32,7 @@ class ReCaptchaComponent extends React.PureComponent {
}
refreshToken() {
if (constants_1.platform.isIOS && this._webViewRef) {
this._webViewRef.injectJavaScript(getExecutionFunction(this.props.siteKey));
this._webViewRef.injectJavaScript(getExecutionFunction(this.props.siteKey, this.props.action));
}
else if (constants_1.platform.isAndroid && this._webViewRef) {
this._webViewRef.reload();
Expand All @@ -43,7 +43,7 @@ class ReCaptchaComponent extends React.PureComponent {
React.createElement(react_native_webview_1.WebView, { ref: (ref) => {
this._webViewRef = ref;
}, javaScriptEnabled: true, originWhitelist: ['*'], automaticallyAdjustContentInsets: true, mixedContentMode: 'always', injectedJavaScript: patchPostMessageJsCode, source: {
html: getInvisibleRecaptchaContent(this.props.siteKey),
html: getInvisibleRecaptchaContent(this.props.siteKey, this.props.action),
baseUrl: this.props.captchaDomain
}, onMessage: (e) => {
this.props.onReceiveToken(e.nativeEvent.data);
Expand Down
2 changes: 2 additions & 0 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type IProps = {
captchaDomain: string
onReceiveToken: (captchaToken: string) => void
siteKey: string
action: string
}

export type IState = {}
Expand All @@ -22,6 +23,7 @@ class ReCaptchaV3 extends React.PureComponent<IProps, IState> {
return (
<ReCaptchaComponent
ref={ref => this._captchaRef = ref}
action={this.props.action}
captchaDomain={this.props.captchaDomain}
siteKey={this.props.siteKey}
onReceiveToken={(token: string) => {
Expand Down
13 changes: 7 additions & 6 deletions src/ReCaptchaComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type IProps = {
captchaDomain: string
onReceiveToken: (captchaToken: string) => void
siteKey: string
action: string
}

const patchPostMessageJsCode = `(${String(function () {
Expand All @@ -19,18 +20,18 @@ const patchPostMessageJsCode = `(${String(function () {
window.postMessage = patchedPostMessage
})})();`

const getExecutionFunction = (siteKey: string) => {
return `window.grecaptcha.execute('${siteKey}', { action: 'login' }).then(
const getExecutionFunction = (siteKey: string, action: string) => {
return `window.grecaptcha.execute('${siteKey}', { action: '${action}' }).then(
function(args) {
window.ReactNativeWebView.postMessage(args);
}
)`
}

const getInvisibleRecaptchaContent = (siteKey: string) => {
const getInvisibleRecaptchaContent = (siteKey: string, action: string) => {
return `<!DOCTYPE html><html><head>
<script src="https://www.google.com/recaptcha/api.js?render=${siteKey}"></script>
<script>window.grecaptcha.ready(function() { ${getExecutionFunction(siteKey)} });</script>
<script>window.grecaptcha.ready(function() { ${getExecutionFunction(siteKey, action)} });</script>
</head></html>`
}

Expand All @@ -40,7 +41,7 @@ class ReCaptchaComponent extends React.PureComponent<IProps> {

public refreshToken() {
if (platform.isIOS && this._webViewRef) {
this._webViewRef.injectJavaScript(getExecutionFunction(this.props.siteKey))
this._webViewRef.injectJavaScript(getExecutionFunction(this.props.siteKey, this.props.action))
} else if (platform.isAndroid && this._webViewRef) {
this._webViewRef.reload()
}
Expand All @@ -58,7 +59,7 @@ class ReCaptchaComponent extends React.PureComponent<IProps> {
mixedContentMode={'always'}
injectedJavaScript={patchPostMessageJsCode}
source={{
html: getInvisibleRecaptchaContent(this.props.siteKey),
html: getInvisibleRecaptchaContent(this.props.siteKey, this.props.action),
baseUrl: this.props.captchaDomain
}}
onMessage={(e: any) => {
Expand Down

0 comments on commit a5830df

Please sign in to comment.