Check the verification code

The final part of the process is to have the user enter the code they received and confirm that it matches the one that was sent by the Verify API.

First, add a new route:

config/routes.rb

Then, create a basic controller:

app/controllers/verifications_controller.rb

Note from the above that is important to skip the before_action we added to the ApplicationController earlier so that the browser doesn’t end up in an infinite loop of redirects.

Create a view to enable the user to fill in their verification code:

app/views/verifications/edit.html.erb

<div class="panel panel-default devise-bs">
  <div class="panel-heading">
    <h4>Verify code</h4>
  </div>
  <div class="panel-body">
    <%= form_tag verification_path(id: params[:id]), method: :put do %>
      <div class="form-group">
        <%= label_tag :code %><br />
        <%= number_field_tag :code, class: "form-control"  %>
      </div>
      <%= submit_tag 'Verify', class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

<%= link_to 'Send me a new code', :root %>

The user then submits their code to the new update action. Within this action you need to take the request_id and code and pass them to the check_verification_request method:

app/controllers/verifications_controller.rb

When the verification check is successful, the user’s status is set to verified and they are redirected to the main page. If the check is unsuccessful, a message displays describing what went wrong

Two-factor authentication for security and spam prevention

Learn how to implement 2fa in your Ruby applications

Steps
1
Introduction
2
Create the basic application
3
Require a phone number
4
Send the verification request
5
Check the verification code
6
Try it out!