Created
November 21, 2018 08:17
-
-
Save shmdt/98829dc61f4bde19d1c629796ad11a99 to your computer and use it in GitHub Desktop.
setup recaptcha-v3 with rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= from_for(@your_instance) do |f| %> | |
<%= button_to 'submit', '', { class: 'btn', onclick: "reCaptcha('your_action')" } %> | |
<%= f.submit 'hide this btn', class: 'hidden', id: 'submit_this_form' %> | |
<% end %> | |
<script src="https://www.google.com/recaptcha/api.js?render=<%= ENV['RECAPTCHA_SITE_KEY'] %>"></script> | |
<script> | |
function reCaptcha(action) { | |
event.preventDefault(); | |
grecaptcha.ready(function () { | |
grecaptcha.execute('<%= ENV['RECAPTCHA_SITE_KEY'] %>', { action: action }).then(function (token) { | |
$('input[name="g-recaptcha-token"]').remove(); | |
$('form').prepend('<input type="hidden" name="g-recaptcha-token" value="' + token + '">'); | |
$('#submit_this_form').click(); | |
}); | |
}); | |
} | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Validatable | |
# thanks https://github.com/igorkasyanchuk/new_google_recaptcha | |
# token = params['g-recaptcha-token'] | |
def recaptcha_validated?(token) | |
uri = URI("https://www.google.com/recaptcha/api/siteverify?secret=#{ ENV['RECAPTCHA_SECRET_KEY'] }&response=#{token}") | |
result = JSON.parse(Net::HTTP.get(uri)) | |
!!result["success"] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can extract url to constant, just looks nicer, something like this: