Sunday, May 19, 2024
HomeRuby On RailsreCAPTCHA v2 | Drifting Ruby

reCAPTCHA v2 | Drifting Ruby


# Terminal
bundle add recaptcha
rails credentials:edit --environment improvement
rails g devise:views
# config/credentials/improvement.yml.enc
recaptcha_site_key: YOUR_GOOGLE_SITE_KEY
recaptcha_secret_key: YOUR_GOOGLE_SECRET_KEY

# or use Setting variables RECAPTCHA_SITE_KEY and RECAPTCHA_SECRET_KEY
# config/initializers/recaptcha.rb
Recaptcha.configure do |config|
  config.site_key = Rails.utility.credentials.recaptcha_site_key
  config.secret_key = Rails.utility.credentials.recaptcha_secret_key
finish
# views/devise/registrations/new.html.erb
  <div class="subject">
    <%= flash[:recaptcha_error] %>
    <%= recaptcha_tags %>
  </div>
# config/routes.rb
devise_for :customers, controllers: { registrations: "customers/registrations" }
# controllers/customers/registrations_controller.rb
class Customers::RegistrationsController < Devise::RegistrationsController
  prepend_before_action :check_captcha, solely: :create

  personal

  def check_captcha
    except verify_recaptcha
      self.useful resource = resource_class.new sign_up_params
      useful resource.validate
      set_minimum_password_length
      respond_with_navigational(useful resource) { render :new }
    finish
  finish
finish
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments