How to reset Google recaptcha with react-google-recaptcha
Asked Answered
H

7

14

Looks like the google recaptcha works in such a way that if a verification attempt has been made with a particular token, it cannot be used again.

Docs states that "you will need to call grecaptcha.reset() to ask the end user to verify with reCAPTCHA again"

I'm trying to attempt this using the react-google-recaptcha npm package.

Here is my code:

function onChange(grecaptcha) {
  console.log(grecaptcha);
  grecaptcha.reset(); // this doesn't work
}

class Captcha extends React.Component {
  render() {
    return <div>
      <Recaptcha
          sitekey='#'
          onChange={onChange(this)}
      /> </div> 
  }
}

When I tried to do the server side validations using the google api https://www.google.com/recaptcha/api/siteverify with response and secret value, the success response always evaluates to "false" after the first validation. To prevent this I'm resetting the grecaptcha as suggested in the docs but it doesn't work.

Anything that I'm missing?

Thanks in advance

EDIT:

https://github.com/dozoisch/react-google-recaptcha offers the reset() utility function which is what I'm trying to call after the user solves the captcha, wondering if I'm not calling it the right way.

Huberman answered 1/10, 2017 at 16:1 Comment(0)
F
8

You can try Reaptcha.

It has more of a react-way approach in dealing with the reCAPTCHA than react-google-recaptcha.

Quoting the documentation:

<Reaptcha
  ref={e => (this.captcha = e)}
  sitekey="YOUR_API_KEY"
  onVerify={() => {
    // Do something
  }}
/>
<button onClick={this.captcha.reset}>
  Reset
</button>
Flub answered 13/6, 2018 at 22:1 Comment(4)
after implementing this recaptcha admin console is still blank, is there anything else to be done?Metcalfe
@Metcalfe I don't understand your question. What do you want to see in the admin console?Flub
I believe reCaptcha's admin console will show some traffic for completed/failed verifications.Metcalfe
@Metcalfe I doubt that the recaptcha admin data is "live" - you'll probably see data after some delay. But anyways - it doesn't really matter if you see that it's working.Flub
V
17

I was having a similar issue, and had to change it to:

window.grecaptcha.reset();
Vesical answered 6/11, 2017 at 0:27 Comment(0)
F
8

You can try Reaptcha.

It has more of a react-way approach in dealing with the reCAPTCHA than react-google-recaptcha.

Quoting the documentation:

<Reaptcha
  ref={e => (this.captcha = e)}
  sitekey="YOUR_API_KEY"
  onVerify={() => {
    // Do something
  }}
/>
<button onClick={this.captcha.reset}>
  Reset
</button>
Flub answered 13/6, 2018 at 22:1 Comment(4)
after implementing this recaptcha admin console is still blank, is there anything else to be done?Metcalfe
@Metcalfe I don't understand your question. What do you want to see in the admin console?Flub
I believe reCaptcha's admin console will show some traffic for completed/failed verifications.Metcalfe
@Metcalfe I doubt that the recaptcha admin data is "live" - you'll probably see data after some delay. But anyways - it doesn't really matter if you see that it's working.Flub
A
4

First set up a suitably scoped variable for the element, then assign it using the ref callback:

let captcha;

<Recaptcha
  sitekey={sitekey}
  onChange={...}
  ref={el => { captcha = el; }}
/>

Then you're able to access the reset function by calling captcha.reset() when you need it (e.g. callback after successful message sent, registering etc.). Hope that helps.

Atheling answered 1/7, 2018 at 12:45 Comment(1)
that is the way to go if you do not want to switch to another liraryAubrette
C
4

Well, this is just a bonus or a more elaborate way of all the above. I am using react-google-recaptcha so this is reactJS and not until I saw this that I found a way to solve the reset issue. It's rather straight forward. Am just explaining to mostly newbies like I think I am on how to do it just as pointed out by @sarneeh, @Chris and @AdityaSrivast

Here's my code snippet.

Captcha.js

import React from "react";
import ReCAPTCHA from "react-google-recaptcha";


const CaptchaVerify = ({yourprops}) => {

 let captcha;

 const onChange = (value) => {
    console.log(value);      
 }

 const setCaptchaRef = (ref) => {
    if (ref) {
      return captcha = ref;
    }
 };

 const resetCaptcha = () => {
   // maybe set it till after is submitted
   captcha.reset();
 }

 return (
   <ReCAPTCHA
     ref={(r) => setCaptchaRef(r) }
     sitekey={process.env.REACT_APP_V2_CAPTCHA_SITE_KEY}
     onChange={onChange, () => resetCaptcha()}
     theme="light"
   />
  )
 };

export default CaptchaVerify;
Ceramist answered 22/10, 2019 at 18:40 Comment(0)
M
3

In TypeScript you can do it like this:

const recaptchaRef = useRef<ReCAPTCHA>(null);
/* make sure it is null at first */

Then:

<ReCAPTCHA
  ref={recaptchaRef}
  sitekey={'key'}
  onChange={(e) => onCaptchaVerify(`${e}`)}
/>;

Then you can use like this after the form submission or where you want to reset the reCaptcha:

  recaptchaRef.current?.reset();
Macegan answered 6/1, 2022 at 20:41 Comment(0)
E
2

google-react-recaptcha follows the syntax:

<ReCAPTCHA
    sitekey="Your client site key"
    onChange={onChange}
/>

You can reset by using reset() component instance. To access it you will need a ref.

Proceed like this:

<ReCAPTCHA
    ref={(r) => this.captcha = r}
    sitekey="Your client site key"
    onChange={onChange}
/>

Now whenever you want to reset the recaptcha, type in:

this.captcha.reset()

and you will be done.

Ideally, one should do it after the form submission, once you have got the response.

Ey answered 8/12, 2018 at 6:35 Comment(1)
reCaptcha.current?.reset() worked for me as i was using const reCaptcha = useRef( null );Syllogism
U
2

Solution with functional component and createRef:

In function body:

  const recaptchaRef = React.createRef();

In JSX:

  <ReCAPTCHA
            ref={recaptchaRef}
            name="recaptcha"
            id="recaptcha"
            sitekey="...."
            onChange={(token)=>{console.log(token);}}
            onExpired={() => {
              recaptchaRef.current.reset(); // here
            }}
            theme="light"
          />
Umbilication answered 31/1, 2021 at 16:38 Comment(1)
For TS users: const recaptchaRef = React.createRef<ReCAPTCHA>();Univalent

© 2022 - 2025 — McMap. All rights reserved.