<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yalinhua</title>
    <description>The latest articles on DEV Community by Yalinhua (@yalinhua).</description>
    <link>https://dev.to/yalinhua</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1104984%2Fb46ff2b5-d7be-4324-a090-59dad1f8797b.jpg</url>
      <title>DEV Community: Yalinhua</title>
      <link>https://dev.to/yalinhua</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yalinhua"/>
    <language>en</language>
    <item>
      <title>Easy way to implement reCAPTCHA V3 in Ruby on Rails</title>
      <dc:creator>Yalinhua</dc:creator>
      <pubDate>Tue, 19 Mar 2024 11:37:08 +0000</pubDate>
      <link>https://dev.to/yalinhua/easy-way-to-implement-recaptcha-v3-in-ruby-on-rails-419g</link>
      <guid>https://dev.to/yalinhua/easy-way-to-implement-recaptcha-v3-in-ruby-on-rails-419g</guid>
      <description>&lt;h2&gt;
  
  
  The difference between reCAPTCHA v2 and v3
&lt;/h2&gt;

&lt;p&gt;You probably remember the tons of "I am not a robot" checkboxes you clicked on. If you were lucky you got passed through, if not you get a little riddle, where you can find all the cars, or stairs or whatever. &lt;/p&gt;

&lt;p&gt;Thats reCAPTCHA v2 - a user interaction based verification that the user is not a bot. &lt;/p&gt;

&lt;p&gt;reCAPTCHA v3 is a pure JavaScript API which you implement to your rails project. It returns a score and gives you the ability to take action in the context of your site. &lt;br&gt;
With reCAPTCHA v3  there is no user interaction needed.&lt;br&gt;
 &lt;br&gt;
Since i am over all the "I am not a robot" verifications i wanted to implement reCAPTCHA v3 to my latest rails project.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to implement reCAPTCHA v3 in rails
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;STEP 1: Add needed Ruby Gems to your Rails Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you don't already have these gems in your gem file, add them and don't forget to run bundle install after you added them. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;gem "dotenv-rails"&lt;/code&gt;&lt;br&gt;
&lt;code&gt;gem "recaptcha", require: "recaptcha/rails"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 2: Create your CAPTCHA API Keys&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go on &lt;a href="https://www.google.com/recaptcha/admin/create"&gt;https://www.google.com/recaptcha/admin/create&lt;/a&gt; and create your API Keys for the domain you want to add the reCAPTCHA for.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 3: Store your CAPTCHA API Keys&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a .env file in your project's root and store your API Keys in it. &lt;/p&gt;

&lt;p&gt;!! Naming convention: If you name your keys exactly like below then they will be read automatically by the gem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RECAPTCHA_SECRET_KEY=********************************
RECAPTCHA_SITE_KEY=********************************
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your working with github, don't forget to add your .env file to .gitignore so that you don't upload the keys to github.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 4: Adjust your method to call the reCAPTCHA verification&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since you are using a contact form which you have already implemented in your project, you should already have a method for the logic for handling your contact form. In my example the method is called contact_send which has a post route connected to it, so the contact form can communicate with it. &lt;/p&gt;

&lt;p&gt;In your reCAPTCHA verification you want to define the parameters for a positive verification and what actions should be executed if these parameters are not met.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unless verify_recaptcha(action: 'checkout', minimum_score: 0.8)
  Rails.logger.error "WARNING: illegal contact form request from \"#{request.remote_ip}\", score: #{recaptcha_reply['score']}, agent: \"#{request.user_agent}\""

  flash[:status] = "invalid-recaptcha"
  render :contact
  return 0
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make your reCAPTCHA verification work properly you have to pass it an action (&lt;a href="https://cloud.google.com/recaptcha-enterprise/docs/actions-website"&gt;Checkout the google actions here&lt;/a&gt; and a minimum score. &lt;/p&gt;

&lt;p&gt;Based on several tests I made, i came to the conclusion that if you don't pass a minimum score, the captcha uses the full range of 0.1–1.0, which makes your CAPTCHA useless.&lt;/p&gt;

&lt;p&gt;I also implemented a Rails.logger to see more details about the error if the captcha does not verify a user and a flash status which will appear and inform the user that the verification was invalid.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 5: Adjust your form .html.erb&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In your .html.erb file you add the following line right above your submit button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%= recaptcha_v3(action: 'checkout') %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you added a flash, you would have to implement it in your .html.erb too.&lt;/p&gt;

&lt;p&gt;I'll upload an article for effectively testing reCAPTCHA V3 soon, so make sure you follow.&lt;/p&gt;

&lt;p&gt;Happy Rails riding ❤&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>ruby</category>
      <category>rails</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
