CloudFlare Turnstile Captchas in Rails Discussion
@Johan André
It seems some gems already exist like the ones below.
https://github.com/pfeiffer/turnstile-captcha
https://github.com/instrumentl/rails-cloudflare-turnstile
I guess these are more "general JS knowledge" questions than specifically turnstile related, but:
I only add the script tag for loading turnstile on my registration page (no need to load it after user registered at this point). The same page tries to connect the turnstile_controller - I often get "ReferenceError: turnstile is not defined", since I guess the turnstile lib has not yet loaded, while the hotwire "stack" is all ready and immediately tries to connect the controller. What would be your suggestion in this case?
Similar issue with disabling the submit button - I often get "this.form is undefined". My guess - the controller is being connected right away, once the dev is found. Seems like it also becomes somewhat of a race condition whether the form is already in the DOM/accessible to JS? E.g. I've moved the "this.button.disabled = true" after turnstile.render call and it seems to work more often. Adding a debugger, I get: "this.form" is undefined, while the "this.element.closest("form")" returns the form.
Maybe I can get around these by changing the script tag from "defer" to be loaded right away, but seems like there should be a way to do "wait until X is defined with a timeout" as well?