I am trying to implement Invisible reCAPTCHA with React and Redux Form. In general, the Invisible reCAPTCHA workflow is:
grecaptcha.execute
with the widget's ID. If necessary, the user will be prompted to solve a challenge. The result is passed to a callback function specified when the CAPTCHA was rendered.I've created a React component intended to be used with Redux Form's Field
that renders the CAPTCHA and updates the form state after grecaptcha.execute
is called:
class ReCaptcha extends React.Component {
render() {
return <div ref={div => this.container=div} />
}
componentDidMount() {
const { input: { onChange }, sitekey } = this.props
grecaptcha.render(this.container, {
sitekey,
size: "invisible",
callback: onChange
})
}
}
However, I don't know how or where to call grecaptcha.execute
together with the widget ID upon form submission by the user. I cannot call it in onSubmit
because the widget ID isn't accessible there. I could call it in ReCaptcha
immediately after rendering the CAPTCHA, but if a user needs to solve the CAPTCHA he'll be prompted to do so as soon as the form has rendered.
This minimal working example shows what I have achieved so far.
Visit the Google reCAPTCHA admin console. Click the '+' icon to register a new site/application. Select captcha version as reCAPTCHA v2 and type as invisible. Copy your site and private keys for further use.
reCAPTCHA is a free service that protects your website from spam and abuse. reCAPTCHA uses an advanced risk analysis engine and adaptive CAPTCHAs to keep automated software from engaging in abusive activities on your site. It does this while letting your valid users pass through with ease.
Unlike v2, reCAPTCHA v3 is invisible for website visitors. There are no challenges to solve. Instead, reCAPTCHA v3 continuously monitors each visitor's behavior to determine whether it's a human or a bot.
The invisible reCAPTCHA badge does not require the user to click on a checkbox, instead it is invoked directly when the user clicks on an existing button on your site or can be invoked via a JavaScript API call. The integration requires a JavaScript callback when reCAPTCHA verification is complete.
Use the onSubmit prop to call grecaptcha.execute(), and point the data-callback to the 'real' onSubmit function.
let refToMyWidget;
const { handleSubmit } = this.props;
componentDidMount() {
if (window.grecaptcha) {
refToMyWidget = window.grecaptcha.render(this.container, {
sitekey: "xxxx",
size: "invisible",
callback: handleSubmit(this.actuallySubmit)
})
}
}
preSubmit() {
if(!window.grecaptcha) {
return;
}
window.grecaptcha.execute(this.refToMyWidget)
}
actuallySubmit() {
// submission logic here
}
render() {
return (
<form onSubmit={handleSubmit(this.preSubmit)}>
<Field name="foo" component="input" />
<button>Submit</button>
</form>
)
}
N.b. I haven't tested this code but it should be more or less right. If you run into trouble with loading grecaptcha into the page/form, I found this code very helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With