Last active
May 31, 2020 19:56
-
-
Save shukerullah/63ba00f1eca4268629f788a387aa3d3a to your computer and use it in GitHub Desktop.
A react native wrapper for google invisible reCAPTCHA v3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import {WebView} from 'react-native-webview'; | |
const getReCaptchaContent = (captchaKey, action) => { | |
const webForm = `<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta | |
name="viewport" | |
content="width=device-width; initial-scale=1; maximum-scale=1" | |
/> | |
<style> | |
.text-xs-center { | |
text-align: center; | |
} | |
.g-recaptcha { | |
display: inline-block; | |
} | |
</style> | |
<script src="https://www.google.com/recaptcha/api.js?render=${captchaKey}"></script> | |
<script type="text/javascript"> | |
grecaptcha.ready(function () { | |
grecaptcha | |
.execute("${captchaKey}", { | |
action: "${action}", | |
}) | |
.then(function (token) { | |
window.ReactNativeWebView.postMessage(token); | |
}); | |
}); | |
</script> | |
</head> | |
</html> | |
`; | |
return webForm; | |
}; | |
type Props = { | |
action?: string, | |
domain: string, | |
captchaKey: string, | |
onSuccess?: Function, | |
}; | |
export default class ReCaptcha extends React.PureComponent<Props> { | |
webview: WebView; | |
static defaultProps = { | |
action: 'verify', | |
onSuccess: () => {}, | |
}; | |
render() { | |
const {domain, captchaKey, action, onSuccess} = this.props; | |
return ( | |
<WebView | |
ref={ref => { | |
this.webview = ref; | |
}} | |
onMessage={({nativeEvent}: Object) => { | |
onSuccess(nativeEvent.data); | |
}} | |
source={{ | |
baseUrl: domain, | |
html: getReCaptchaContent(captchaKey, action), | |
}} | |
/> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A react native wrapper for google invisible reCAPTCHA v3
Example: