ambethia / recaptcha

ReCaptcha helpers for ruby apps
http://github.com/ambethia/recaptcha
MIT License
1.96k stars 439 forks source link

TypeError: Cannot set properties of null at setInputWithRecaptchaResponseTokenForConnectProvider #424

Closed pritambios closed 1 year ago

pritambios commented 1 year ago

Rails version 7 Using with Turbo.

Error in browser console

VM366:11 Uncaught (in promise) TypeError: Cannot set properties of null (setting 'value')
    at setInputWithRecaptchaResponseTokenForConnectProvider (<anonymous>:11:27)
    at <anonymous>:5:17

Code snippet where the issue is happening

function executeRecaptchaForConnectProvider() {
            grecaptcha.ready(function() {
              grecaptcha.execute('<hidden site key>', {action: 'connect_provider'}).then(function(token) {
                setInputWithRecaptchaResponseTokenForConnectProvider('g-recaptcha-response-data-connect-provider', token)
              });
            });
          };
                    var setInputWithRecaptchaResponseTokenForConnectProvider = function(id, token) {
            var element = document.getElementById(id);
            element.value = token; // element is null
          }
victor-fdez commented 1 year ago

Hi @pritambios , I hope you're doing well.

I wanted to reach out and let you know that I've submitted a pull request #425 that solves an issue with the executeRecaptchaForConnectProvider function. The issue was that sometimes the function would throw an error when it couldn't find an element by its ID, which would happen in our case when a user closed a modal which had repatcha_v3 embedded in it and it couldn't find the hidden input tag.

The PR adds the optional ignore_no_element: true option to handle this issue.

Hope this helps!

grosser commented 1 year ago

5.13.0 🎉