Reputation: 416
I have an app with Devise authentication, i installed the gem 'recaptcha', and override the registrations controller that looks like:
class RegistrationsController < Devise::RegistrationsController
prepend_before_action :check_captcha, only: [:create] # Change this to be any actions you want to protect.
private
# Modified Devise params for user login
def sign_up_params
params.require(:merci).permit(:razon_social, :rfc, :email, :password, :password_confirmation)
end
def after_sign_up_path_for(resource)
"/home/index"
end
def after_inactive_sign_up_path_for(resource)
"/home/index"
end
private
def check_captcha
puts "Check"
puts verify_recaptcha
unless verify_recaptcha
self.resource = resource_class.new sign_up_params
resource.validate # Look for any other validation errors besides Recaptcha
set_minimum_password_length
redirect_to controller: 'registrations', action: 'new'
end
end
end
And my devise layout looks like:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), class: "") do |f| %>
<div class="panel panel-body login-form">
<% if notice %>
<div class="alert bg-success alert-styled-left">
<button type="button" class="close" data-dismiss="alert"><span>×</span><span class="sr-only">Close</span></button>
<%= notice %>
</div>
<% end %>
<% if alert %>
<div class="alert bg-warning alert-styled-left">
<button type="button" class="close" data-dismiss="alert"><span>×</span><span class="sr-only">Close</span></button>
<%= alert %>
</div>
<% end %>
<%= devise_error_messages! %>
<%= flash[:recaptcha_error] %>
<div class="text-center">
<img src="https://s3.amazonaws.com/merci-comensalcdn/logos/merci_logo_white.png" class="img-responsive" width="100px" style="margin: 0 auto;">
<h5 class="content-group-lg">Registro Merci<small class="display-block">Ingresa tus credenciales de registro</small></h5>
</div>
<div class="form-group has-feedback has-feedback-left">
<%= f.text_field :rfc, autocomplete: "off", autofocus: false, class: "form-control", placeholder: "RFC", value: "", required: true %>
<div class="form-control-feedback">
<i class="fa fa-building text-muted"></i>
</div>
</div>
<div class="form-group has-feedback has-feedback-left">
<%= f.text_field :razon_social, autocomplete: "off", autofocus: false, class: "form-control", value: "", placeholder: "Razón Social", required: true %>
<div class="form-control-feedback">
<i class="fa fa-building text-muted"></i>
</div>
</div>
<div class="form-group has-feedback has-feedback-left">
<%= f.email_field :email, autofocus: false, class: "form-control", placeholder: "Email", required: true %>
<div class="form-control-feedback">
<i class="fa fa-user text-muted"></i>
</div>
</div>
<div class="form-group has-feedback has-feedback-left">
<%= f.password_field :password, autocomplete: "off", class: "form-control", placeholder: "Contraseña", required: true %>
<div class="form-control-feedback">
<i class="fa fa-lock text-muted"></i>
</div>
</div>
<div class="form-group has-feedback has-feedback-left">
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control", placeholder: "Repetir contraseña", required: true %>
<div class="form-control-feedback">
<i class="fa fa-lock text-muted"></i>
</div>
</div>
<div class="form-group has-feedback has-feedback-left">
<%= recaptcha_tags %>
</div>
<div class="form-group login-options">
<div class="row">
<div class="col-sm-6">
</div>
<div class="col-sm-6 text-right">
<a href="/login">Ya tengo cuenta</a>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block btn-loading"> Registrarme</button>
</div>
<span class="help-block text-center no-margin">Tu acceso es unico y será monitoreado.</a></span>
</div>
<% end %>
I add the domains on the captcha config page "localhost" and "127.0.0.1" and all necesary to work on local env. So when i always check the param "verify_recaptcha" always return false, never let me continue on the process.
And my log shows the next:
Started POST "/register" for 127.0.0.1 at 2018-07-19 17:47:21 -0500
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"gdrfARR4iB49kGWI8BHONw+W+xw6bwlcgb/6z11URhPl0u+wDjWDsQ1/Wp8GzME9md1Bt/ZZa1CIgG+w8otHHA==", "merci"=>{"rfc"=>"test", "razon_social"=>"test", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "g-recaptcha-response"=>"03AEMEkEm5-RXF-bXsk8ppQJgvSOfRc4Xp0GUPMU33TXxjBPJPrXQYEiS3WPgwi91RSmJXGRs6QbNTTpiA0mLH021wvWzAfCpwA2jC8NXjU2aaOu0vPckIabMPU9WveXVgKUXBAGp_88Fw6GmlgKfZZu7CB8SX4qtBDgZuJ4Dv1eoNX3iADe5_DjHhybm9QDZi5T4TnuwyI4-ItYCv6WaSlPzNrhbvUGX4qdLC9OwzoJk-r_OiyKkCFJbn4oouDZco3RPzFd5B8be0qEYWkaY4FlTxwJ714BgMfQ"}
Check
false
Merci Exists (50.2ms) SELECT 1 AS one FROM "mercis" WHERE "mercis"."email" = $1 LIMIT $2 [["email", "[email protected]"], ["LIMIT", 1]]
↳ app/controllers/registrations_controller.rb:27
Redirected to http://localhost:3000/register
Filter chain halted as :check_captcha rendered or redirected
Completed 302 Found in 630ms (ActiveRecord: 237.2ms)
Started GET "/register" for 127.0.0.1 at 2018-07-19 17:47:21 -0500
Processing by RegistrationsController#new as HTML
Rendering devise/registrations/new.html.erb within layouts/devise_merci_register
Rendered devise/shared/_links.html.erb (1.1ms)
Rendered devise/registrations/new.html.erb within layouts/devise_merci_register (7.3ms)
Completed 200 OK in 68ms (Views: 67.2ms | ActiveRecord: 0.0ms)
Any advice?
Upvotes: 0
Views: 651
Reputation: 416
The problem was on the captcha function, on rails 5 it works like:
def create
if !verify_recaptcha
flash.delete :recaptcha_error
build_resource(sign_up_params)
resource.valid?
resource.errors.add(:base, "There was an error with the recaptcha code below. Please re-enter the code.")
clean_up_passwords(resource)
respond_with_navigational(resource) { render :new }
else
flash.delete :recaptcha_error
super
end
end
Upvotes: 1