Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executeRecaptcha never resolve #81

Closed
Kif11 opened this issue May 12, 2021 · 8 comments
Closed

executeRecaptcha never resolve #81

Kif11 opened this issue May 12, 2021 · 8 comments

Comments

@Kif11
Copy link

Kif11 commented May 12, 2021

If recaptcha token is not specified executeRecaptcha() promise doesn't resolve or reject. In the example below token or error is never printed.

try {
  const recaptchaToken = await executeRecaptcha('signup_modal');
  console.log('token', recaptchaToken);
} catch (error) {
  console.log(error)
}

This behavior can be described by the following example code.

const simple = async () => {
  console.log('start')
  await new Promise(resolve => {})
  console.log('done.')
}

I'm using "react-google-recaptcha-v3": "^1.7.1"

@t49tran
Copy link
Owner

t49tran commented May 13, 2021

@Kif11, what the exact version you are using ? What is the version in your yarn.lock or package-lock.json ?

@Kif11
Copy link
Author

Kif11 commented May 16, 2021

@t49tran

cat yarn.lock | grep capt
react-google-recaptcha-v3@^1.7.1:
  resolved "https://registry.yarnpkg.com/react-google-recaptcha-v3/-/react-google-recaptcha-v3-1.7.1.tgz#e49f81e42b59c04ab9b488b88db3dd6e41e7aa00"

One of my teammates verified this behavior on his machine. I didn't take time to set up isolated test though.

@t49tran
Copy link
Owner

t49tran commented May 18, 2021

@Kif11, I am not sure what you mean by saying If recaptcha token is not specified ? Also, how do you get access to executeRecaptcha ?

The piece of code you given doesn't provide me with enough information to debug anything ? If it's possible, can you share the code and how you use the GoogleRecaptchaProvider and get access to executeRecaptcha.

@t49tran
Copy link
Owner

t49tran commented May 18, 2021

Also, you might want to help a look at this issue #56

@KarlBaumann
Copy link

executeRecaptcha is undefined for me
image

@t49tran
Copy link
Owner

t49tran commented May 23, 2021

executeRecaptcha is undefined for me
image

The executeRecaptcha function is undefined while the javascript is being loaded, please do a null check an call it only when is it available. For your use case, if you want to trigger it on page load, you can do something like:

const {executeRecaptcha} = useGoogleReCaptcha();

useEffect(() => {
    if (!executeRecaptcha) {
       return;
   }

  const verifyRecaptcha = async() => {
      const recaptchaToken = await executeRecaptcha("login_page");
  };
  
 verifyRecaptcha();
}, [executeRecaptcha]);

That section in ReadMe.md has not been updated for a while and I will update it soon.

@t49tran
Copy link
Owner

t49tran commented May 23, 2021

Doc updated with #84

@t49tran t49tran closed this as completed Jun 18, 2021
@egolegegit
Copy link

Thanks for answering the question above. This solved my problem too :) !
Here is the code running nextjs, form component in modal window, maybe it will be useful to others:

const { executeRecaptcha } = useGoogleReCaptcha();

    useEffect(() => {
        if (!executeRecaptcha) {
            return;
        }

        const verifyRecaptcha = async () => {
            const recaptchaToken = await executeRecaptcha('order_form');
        };

        verifyRecaptcha();
    }, [executeRecaptcha]);

    const handleSubmitForm = useCallback(
        async (values) => {
            if (!executeRecaptcha) {
                console.log('Execute recaptcha not yet available');
                return;
            }

            const gReCaptchaToken = await executeRecaptcha('OrderForm');
            await submitEnquiryForm(gReCaptchaToken, values);
        },
        [executeRecaptcha],
    );

    const submitEnquiryForm = async (gReCaptchaToken, values) => {
        fetch('/api/captchaEnquiry', {
            method: 'POST',
            headers: {
                Accept: 'application/json, text/plain, */*',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                data: { ...values },
                gRecaptchaToken: gReCaptchaToken,
            }),
        })
            .then((res) => res.json())
            .then((res) => {
                // console.log(res, 'response from backend');
                if (res?.status === 'success') {
                    setNotification(res?.message);
                    notifications.show({
                        title: res?.message,
                        message: '',
                        icon: <ShieldCheck size="1rem" />,
                        color: 'teal',
                    });
                    close();
                } else {
                    setNotification(res?.message);
                }
            });
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants