Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Invisible reCAPTCHA with Redux Form

I am trying to implement Invisible reCAPTCHA with React and Redux Form. In general, the Invisible reCAPTCHA workflow is:

  1. Render the "invisible" CAPTCHA, returning its widget ID.
  2. Call 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.
  3. Submit the form together with the CAPTCHA result.

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.

like image 419
csm Avatar asked Feb 10 '17 01:02

csm


People also ask

How do you implement the invisible reCAPTCHA react?

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.

Is invisible reCAPTCHA free?

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.

Is v3 Captcha invisible?

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.

How does reCAPTCHA v3 invisible work?

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.


1 Answers

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.

like image 106
TwinkleFairyGlitterPuff Avatar answered Oct 02 '22 18:10

TwinkleFairyGlitterPuff