[Rails] How to install reCAPTCHA

Target

ezgif.com-video-to-gif.gif

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

The following has been implemented.

Slim introduction -Login function implementation

Register reCAPTCHA

1. Access the link below

reCAPTCHA

2. Click ʻAdmin console`

スクリーンショット 2020-06-11 19.59.07.png

3. Set as shown in the image below and click Send

スクリーンショット 2020-06-11 20.02.04.png

スクリーンショット 2020-06-11 20.02.09.png

4. Make a copy of the site key and secret key and make a note of them.

スクリーンショット 2020-06-11 20.07.55.png

Implementation

1. Make site key and secret key environment variables

** ① Introduced "gem'dotenv-rails'" **

Gemfile


gem 'dotenv-rails'

Terminal


& bundle

** ② Create a ".env" file directly under the application **

Terminal


$ touch .env 

スクリーンショット 2020-06-02 11.32.54.png

** ③ Edit the .env file **

.env


#Postscript
RECAPTCHA_SITE_KEY = 'Site key'
RECAPTCHA_SECRET_KEY = 'Secret key'

** ④ Edit the .gitignore file **

.gitignore


/.env #Postscript

2. Create / edit reCAPTCHA configuration file

Terminal


$ touch config/initializers/recaptcha.rb

recaptcha.rb


#Postscript
Recaptcha.configure do |config|
  config.site_key = ENV["RECAPTCHA_SITE_KEY"]
  config.secret_key = ENV["RECAPTCHA_SECRET_KEY"]
end

3. Edit resistration_controller.rb

resistration_controller.rb


#Postscript
prepend_before_action :check_captcha, only: [:create]

private

  #If you do not perform reCAPTCA authentication, you will not be able to sign up and a validation message will be displayed.
  def check_captcha
    self.resource = resource_class.new sign_up_params
    resource.validate
    unless verify_recaptcha(model: resource)
      respond_with_navigational(resource) { render :new }
    end
  end

4. Edit the view

slim:resistrations/new.html.slim


/Postscript
= recaptcha_tags

5. Japanese localization of validation message

** ① Introducing Gem **

Gemfile


gem 'rails-i18n'
gem 'devise-i18n'

Terminal


$ bundle

** ② Edit ʻapplication.rb` **

application.rb


module Bookers2Debug
  class Application < Rails::Application
    config.load_defaults 5.2
    config.i18n.default_locale = :ja #Postscript
  end
end

** ③ Create / edit devise.ja.yml file **

yml:devise.ja.yml


ja:
  recaptcha:
    errors:
      verification_failed: 'ReCAPTCHA authentication failed.'

Recommended Posts

[Rails] How to install reCAPTCHA
[Rails] How to install devise
[Rails] How to install Font Awesome
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
How to install Swiper in Rails
How to write Rails
How to install Docker
How to uninstall Rails
How to install docker-machine
How to install MySQL
How to install ngrok
[Ruby on Rails] How to install Bootstrap in Rails
[rails] How to post images
[Rails] How to use enum
[Rails] How to use enum
How to read rails routes
How to install Boots Faces
How to use rails join
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
How to install JDK8-10 (Mac)
[Rails] How to use Scope
[Rails] How to install a decorator using gem draper
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
Rails new fails to install mysql
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[rails] How to display db information
How to install JMeter for Mac
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
How to install ruby through rbenv
[How to install Spring Data Jpa]
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
Super rough! How to install Dagger2
[Rails] How to add new pages
How to install Bootstrap in Ruby
How to install MariaDB 10.4 on CentOS 8
Rails on Tiles (how to write)
How to install WildFly on Ubuntu 18.04
How to introduce jQuery in Rails 6
[Rails] How to implement star rating
How to get along with Rails
[Introduction to Rails] How to use render
Super easy in 2 steps! How to install devise! !! (rails 5 version)
How to implement search functionality in Rails
How to change app name in rails
Install Webpacker and Yarn to run Rails
How to install Play Framework 2.6 for Mac