Remove duplication of recaptcha code, which caused issues, and improved script tag logic to check for errors.

This commit is contained in:
Jonathan Rosenbaum 2026-01-31 17:30:03 +00:00
parent e29832c202
commit 32f95d7ae4
2 changed files with 27 additions and 17 deletions

View File

@ -30,19 +30,31 @@ module ApplicationHelper
} }
end end
def recaptcha_execute(action) def recaptcha_execute(action)
id = "recaptcha_token_#{SecureRandom.hex(10)}" id = "recaptcha_token_#{SecureRandom.hex(10)}"
raw %Q{ raw %Q{
<input name="recaptcha_token" type="hidden" id="#{id}"/> <input name="recaptcha_token" type="hidden" id="#{id}"/>
<script> <script>
grecaptcha.ready(function() { if (typeof grecaptcha !== 'undefined') {
grecaptcha.execute('#{RECAPTCHA_SITE_KEY}', {action: '#{action}'}).then(function(token) { grecaptcha.ready(function() {
document.getElementById("#{id}").value = token; grecaptcha.execute('#{RECAPTCHA_SITE_KEY}', {action: '#{action}'}).then(function(token) {
}); document.getElementById("#{id}").value = token;
}); }).catch(function(error) {
</script> console.error('reCAPTCHA execution error:', error);
} });
end });
} else {
document.addEventListener('grecaptcha-ready', function() {
grecaptcha.execute('#{RECAPTCHA_SITE_KEY}', {action: '#{action}'}).then(function(token) {
document.getElementById("#{id}").value = token;
}).catch(function(error) {
console.error('reCAPTCHA execution error:', error);
});
});
}
</script>
}
end
end end

View File

@ -69,9 +69,7 @@
.dlg-inner .dlg-inner
.message='' .message=''
%button.close=_'modals.done_button' %button.close=_'modals.done_button'
- if @login_dlg.present? - if @login_dlg.present?
= content_for :recaptcha_js do
= include_recaptcha_js
.dlg#login-dlg .dlg#login-dlg
.dlg-content .dlg-content
%h2.title=_'forms.actions.generic.login' %h2.title=_'forms.actions.generic.login'