```
--------------------------------
### reCAPTCHA v2 Verification
Source: https://github.com/ambethia/recaptcha/blob/master/README.md
Verifies the user's reCAPTCHA submission in your controller. It returns true if the submission is valid, and false otherwise. An exception is raised on failure when using `verify_recaptcha!`.
```APIDOC
## verify_recaptcha
### Description
Verifies the user's reCAPTCHA submission in your controller. Returns true on success, false on failure. Raises an exception on failure when using `verify_recaptcha!`.
### Usage
```ruby
# In your controller:
if verify_recaptcha(model: @user) && @user.save
redirect_to @user
else
render 'new'
end
```
```
--------------------------------
### Configure recaptcha gem for Cloudflare Turnstile
Source: https://github.com/ambethia/recaptcha/wiki/Cloudflare-Turnstile
Update your Rails application's recaptcha configuration to use Cloudflare Turnstile's verification and API server URLs. Ensure no compatibility layer is included in the verify_url.
```ruby
Recaptcha.configure do |config|
# Cloudflare Turnstile
config.verify_url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify'
config.api_server_url = 'https://challenges.cloudflare.com/turnstile/v0/api.js'
end
```
--------------------------------
### Re-render Form with Validation Errors and v2 Recaptcha
Source: https://github.com/ambethia/recaptcha/wiki/Recaptcha-with-Turbo-and-Stimulus
In case of validation errors or Recaptcha failure, re-render the form and replace the recaptcha frame with the v2 version. This ensures all errors are displayed to the user.
```html
<%= turbo_stream.replace dom_id(@user), partial: 'users/form', locals: {user: @user} %>
<%= turbo_stream.replace 'recaptcha' do %>
<% end %>
```
--------------------------------
### Monkey-patch recaptcha gem for POST verification
Source: https://github.com/ambethia/recaptcha/wiki/Cloudflare-Turnstile
This code monkey-patches the recaptcha gem to enable server-side verification calls via HTTP POST, which is required for Cloudflare Turnstile. This is only needed for versions less than 5.15.0 of the gem.
```ruby
module Recaptcha
def self.api_verification_free(verify_hash, timeout: nil)
uri = URI.parse(configuration.verify_url)
http_instance = http_client_for(uri: uri, timeout: timeout)
request = Net::HTTP::Post.new(uri.request_uri)
request['Content-Type'] = 'application/json; charset=utf-8'
request.body = JSON.generate(verify_hash)
JSON.parse(http_instance.request(request).body)
end
end
```
--------------------------------
### reCAPTCHA Reply
Source: https://github.com/ambethia/recaptcha/blob/master/README.md
Retrieves the reCAPTCHA response from the request. This is a lower-level method that can be used to access the raw reCAPTCHA response data.
```APIDOC
## recaptcha_reply
### Description
Retrieves the reCAPTCHA response from the request. This is a lower-level method that can be used to access the raw reCAPTCHA response data.
### Usage
```ruby
recaptcha_response = recaptcha_reply
```
```
--------------------------------
### Display Recaptcha v3 in a Turbo Frame
Source: https://github.com/ambethia/recaptcha/wiki/Recaptcha-with-Turbo-and-Stimulus
Embed Recaptcha v3 within a Turbo Frame tag in your form. Ensure the `turbo: true` option is set for proper integration.
```html
<%= turbo_frame_tag user, target: '_top' do %>
<%= form_with model: user do |f| %>
<%= turbo_frame_tag 'recaptcha' do %>
<%= recaptcha_v3 action: 'signup', site_key: ENV['RECAPTCHA_SITE_KEY_V3'], turbo: true %>
<% end %>
<%= f.submit 'Submit' %>
<% end %>
<% end %>
```
--------------------------------
### reCAPTCHA v3 Verification
Source: https://github.com/ambethia/recaptcha/blob/master/README.md
Verifies reCAPTCHA v3 submissions. This method should be used in conjunction with reCAPTCHA v3 to validate user actions based on the score provided.
```APIDOC
## verify_recaptcha (use with v3)
### Description
Verifies reCAPTCHA v3 submissions. This method should be used in conjunction with reCAPTCHA v3 to validate user actions based on the score provided.
### Usage
```ruby
# In your controller:
if verify_recaptcha(model: @user, score: 0.5) && @user.save
redirect_to @user
else
render 'new'
end
```
```
--------------------------------
### reCAPTCHA v3 Score
Source: https://github.com/ambethia/recaptcha/blob/master/README.md
Provides a score for the user's interaction, used for verifying requests with reCAPTCHA v3. This method is used when your reCAPTCHA key is configured for v3.
```APIDOC
## recaptcha_v3
### Description
Provides a score for the user's interaction, used for verifying requests with reCAPTCHA v3.
### Usage
```ruby
# Example usage within a controller action
score = recaptcha_v3(action: 'homepage')
if score > 0.5
# Proceed with action
else
# Handle suspicious activity
end
```
```
--------------------------------
### Add Turnstile class to recaptcha tag
Source: https://github.com/ambethia/recaptcha/wiki/Cloudflare-Turnstile
Force the addition of the 'cf-turnstile' classname to the recaptcha tag to enable Turnstile. This ensures Turnstile is used instead of reCAPTCHA.
```ruby
recaptcha_tags class: 'cf-turnstile'
```
--------------------------------
### Reset reCAPTCHA Widget
Source: https://github.com/ambethia/recaptcha/blob/master/README.md
If you need to reset the captcha and generate a new response token after a submission or timeout, call the JavaScript function `grecaptcha.reset()`.
```javascript
grecaptcha.reset()
```
--------------------------------
### reCAPTCHA v2 Tags
Source: https://github.com/ambethia/recaptcha/blob/master/README.md
Embeds the necessary JavaScript and HTML for the reCAPTCHA v2 checkbox widget in your views. This method is used when your reCAPTCHA key is configured for the 'I'm not a robot' checkbox.
```APIDOC
## recaptcha_tags
### Description
Embeds the necessary JavaScript and HTML for the reCAPTCHA v2 checkbox widget in your views.
### Usage
```erb
<%= recaptcha_tags %>
```
```
--------------------------------
### Error Handling Method for AJAX Responses
Source: https://github.com/ambethia/recaptcha/wiki/Add-form-with-Turbolinks,-Recaptcha,-and-errors-handling
A utility function to replace the current page's HTML with the content from an AJAX error response. This is useful for displaying form errors or other server-side messages without a full page reload.
```javascript
// javascript/per_page/methods/on_error_replace_page_with_recived_respons.js
export const on_error_replace_page_with_recived_respons = (data) -> {
$(body).html(data.body.innerHTML);
}
// load will load the scripts again
Turbolinks.dispatch 'turbolinks:load' # will reload js if this is how your turbolinks works
scrollTo(0, 0)
```