Thursday, 23 January 2020

reCaptcha

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.
        
  1. class User::RegistrationsController < Devise::RegistrationsController
  2. #
  3. # Other Codes
  4. def create
  5. ## To build the resource
  6. build_resource(sign_up_params)
  7. ## Verifying Captcha
  8. if verify_recaptcha(model: resource)
  9. super
  10. else
  11. render 'new'
  12. end
  13. end
  14. #
  15. # Other Codes
  16. 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
     
  1. Recaptcha.configure do |config|
  2. config.public_key = 'Replace with your public key'
  3. config.private_key = 'Replace with private key'
  4. 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

  Common Git Commands   Take clone of a repository git clone <remote-repository-url> ex: git clone  https://github.com/agricor/RegTech...