I would like to install google recaptchaV3. I followed this guide step by step https://github.com/heartcombo/devise/wiki/How-To:-Use-Recaptcha-with-Devise.
On sign_up, the line "return if verify_recaptcha(action: 'signup')" in registrations_controller return 405 "Method Not Allowed"
recaptcha.rb
Recaptcha.configure do |config|
config.site_key = Rails.application.credentials.dig(:google, :RECAPTCHA_PUBLIC_KEY)
config.secret_key = Rails.application.credentials.dig(:google, :RECAPTCHA_PRIVATE_KEY)
config.proxy = "http://www.google.com/recaptcha/api/verify"
end
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: {
registrations: 'registrations',
omniauth_callbacks: 'users/omniauth_callbacks',
passwords: 'users/passwords'
}
end
devise/registrations/new.html.erb
<%= simple_form_for(resource, html: { class: "mt-8 space-y-4" },
as: resource_name,
url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<%= flash[:recaptcha_error] %>
<%= recaptcha_v3(action: 'signup') %>
<%= f.input :email %>
<%= f.input :pseudo %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= f.button :submit %>
<% end %>
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController #:nodoc:
prepend_before_action :check_captcha, only: [:create]
private
def sign_up_params
params.require(:user).permit(:pseudo, :email, :password, :password_confirmation)
end
def account_update_params
params.require(:user).permit(:pseudo, :email, :password, :password_confirmation, :current_password)
end
def after_update_path_for(resource)
user_path(resource)
end
private
def check_captcha
return if verify_recaptcha(action: 'signup')
self.resource = resource_class.new sign_up_params
resource.validate # Look for any other validation errors besides reCAPTCHA
set_minimum_password_length
respond_with_navigational(resource) do
flash.discard(:recaptcha_error) # We need to discard flash to avoid showing it on the next page reload
render :new
end
end
end
Just remove : config.proxy = "http://www.google.com/recaptcha/api/verify" in recaptcha.rb