ReCaptcha
1. Open your Gemfile and add this code:
gem "recaptcha", require: "recaptcha/rails"
2. Now run bundle
bundle install
3- Then I inherited devise registration controller and overridden the create method of it.
class User::RegistrationsController < Devise::RegistrationsController
#
# Other Codes
- def create
## To build the resource
build_resource(sign_up_params)
## Verifying Captcha
- if verify_recaptcha(model: resource)
super
else
render 'new'
end
end
- #
# Other Codes
end
4. Now you need to get the public and secret key by registering into Google recaptcha account.
1- Label - recaptcha Test
2- reCAPTCHA type - reCAPTCHA v2 and after select 1 option
("I'm not a robot" tickbox)
3- Domains - localhost, for heroku- heroku.com
5. Now add the configurations for recaptcha at /config/initializer/recaptcha.rb
Recaptcha.configure do |config|
config.public_key = 'Replace with your public key'
config.private_key = 'Replace with private key'
end
OR
add .env file in project source add key
export RECAPTCHA_SITE_KEY = '6LcLFdIUAAAAAFKQUU4b3HnWYtssgTWdsnQZsVXU'
export RECAPTCHA_SECRET_KEY = '6LcLFdIUAAAAAA4Z66WTWojY0_RsyR7DqnhtOYUf'
6. Add the recaptcha tags to app/views/devise/registrations/new
<div><%= recaptcha_tags %></div>
7. Now we need to tell the route to go to our registration controller instead of devise registration controller. So routes needs to be modified to:
devise_for :users, :controllers => {:registrations => "user/registrations"}
Link for reCaptch:-
No comments:
Post a Comment