DEV Community

Discussion on: Rails Devise and ReCaptcha with Hotwire (Turbo and Stimulus)

Collapse
 
zachgl profile image
zachGl

Hi, thank you for the nice tutorial.
However my browser complains that grecaptcha is undefined in load_recaptcha_v2_controller.js

Have you experienced the same error?

Collapse
 
fadrien profile image
Francois Adrien • Edited

Hi Zach, sorry for the late reply.
I hope you found your answer already ! Anyway here are some clues for troubleshooting this behaviour.

Context: In this tutorial, the grecaptcha variable is initialized from the script generated by the recaptcha_v3 tag on first page load.
Then, in case the Recaptcha V3 score is lower than expeted, the Recaptcha V2 is rendered through Turbo Stream, which allows the javascript from the recaptcha_v3 tag to remain available for the load_recaptcha_v2_controller.js and thus, access the grecaptcha variable.

Solution: If you are either rendering Recaptcha V2 without rendering Recaptcha V3 first, or if you do a full page reload when rendering Recaptcha V2, just add the Recaptcha JS API manually with adding the <script src="https://www.recaptcha.net/recaptcha/api.js" async="" defer=""></script> line in your HTML.

I hope it helped !