Skip to content

About

Integrate Cloudflare Turnstile into Ruby on Rails apps with ease.

Resources

Stars

44 stars

Watchers

1 watching

Forks

Latest commit

 

History

93 Commits

Folders and files

Repository files navigation

Turnstiled

Integrate Cloudflare Turnstile into Ruby on Rails apps with ease.

For development, it even includes a Mock widget.

Getting Usage

Installation

First install the gem by adding this line to your application's Gemfile:

gem "turnstiled"

And executing:

$ bundle

Configuration

Create a file called config/initializers/turnstiled.rb and add the following:

Turnstiled.site_key = "YOUR SITE KEY"
Turnstiled.site_secret = "YOUR SITE SECRET"

In your layout, add the widget to the head of the document:

<%= turnstile_javascript_tag %>

In test and development, it will include a mock widget that fires the callback after 2 seconds.

The mock widget will send the cf-turnstile-response parameter with a value of 1.

Within a form make sure to render the widget:

<%= form_with model: @some_model do %>
  <%= turnstile_tag %>
<% end %>

In your controller, you also need to verify the response with the following:

class PostsController < ApplicationController
  verify_turnstile_request only: %i[create]
end

The verify_turnstile_request takes the same options as a before_action method call.

A failed check answers 400 Bad Request. To re-render the form instead, name a method with with:, or pass a block:

class SessionsController < ApplicationController
  verify_turnstile_request with: :turnstile_failed, only: %i[create]

  private

  def turnstile_failed
    @session = Session.new
    @session.errors.add(:base, "That check did not go through. Try again.")

    render :new, status: :unprocessable_content
  end
end
verify_turnstile_request(only: %i[create]) { redirect_to root_path }

When Cloudflare can't be reached, the error is reported through Rails.error and the request is treated as a failed check.

Releasing

Change the version in lib/turnstiled/version.rb and then run bundle to make sure that Gemfile.lock is updated with the new version.

The gem is automatically released when pushed to main on GitHub

About

Integrate Cloudflare Turnstile into Ruby on Rails apps with ease.

Resources

Stars

44 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages