Mobile Number Verification Using 2FACTOR in Rails Application

module MobileVerification
class TwoFactor
BASE_URL = 'http://2factor.in/API/V1'
API_KEY = 'Your APi Key here'
class << self
def send_otp(opts: {})
uri = "#{vendor_base_url}/SMS/#{opts[:mobile_number]}/AUTOGEN"
url = URI(uri)
send_request(url)
end
def verify_otp(opts: {})
url = URI("#{vendor_base_url}/SMS/VERIFY/#{opts[:session_id]}/#{opts[:otp]}")
send_request(url)
end
private def send_request(url)
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Get.new(url)
request["content-type"] = 'application/x-www-form-urlencoded'
response = http.request(request)
rescue => e
render_errors(e.message)
else
render_response(response)
end
def render_response(response)
body = JSON.parse(response.read_body)
if body['Status'] == 'Error' || (body['Status'] == 'Success' && body['Details'] == 'OTP Expired')
render_errors(body['Details'])
else
render_success(body['Details'])
end
end
def render_errors(details)
{ success: false, details: details }
end
def render_success(details)
{ success: true, details: details }
end
def vendor_base_url
BASE_URL + '/' + API_KEY
end
end
end
end
module Api
module V1
class MobileVerificationsController < ApiController
def send_otp
mobile_number = params.dig(:mobile_verification, :mobile_number)
if mobile_number.present?
response = MobileVerification::TwoFactor.send_otp(
opts: {
mobile_number: mobile_number
}
)
if response[:success]
render json: response, status: :ok
else
render_unprocessable_entity('Failed to send OTP')
end
else
render_unprocessable_entity('mobile number is missing')
end
end
def verify_otp
session_id = params.dig(:mobile_verification, :session_id)
otp = params.dig(:mobile_verification, :otp)
if session_id && otp
response = MobileVerification::TwoFactor(
opts: {
session_id: session_id,
otp: otp
}
)
if response[:success]
render json: response, status: :ok
else
render_unprocessable_entity('Failed to verify OTP')
end
else
render_unprocessable_entity('session_id or otp is missing')
end
end
private def render_unprocessable_entity(message = nil)
render json: { message: message }, status: :unprocessable_entity
end
end
end
end
{
"Status": "Success",
"Details": "ff9ab5b0-452e-11e7-94da-0200cd936042"
}
{
"Status": "Success",
"Details": "OTP Matched"
}

--

--

--

Ruby on Rails + ReactJs Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Create a Simple WordPress Plugin

Use AWS SES with GoDaddy and Gatsby

Configure Hadoop Cluster Using Ansible Playbook..

Introduction to Genetic Algorithms

Build an Infrastructure in AWS using Terraform.

5 Books Every Web Developer Should Read Before the End of 2020

what i have been waiting for has come !

Week 9— Building Something from scratch [2021/12/13]

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akashkinwad

Akashkinwad

Ruby on Rails + ReactJs Developer

More from Medium

What is Package-lock.json for anyway?

Building A Live Streaming Tic Tac Toe Game

Easy Checklist To Plan a Successful App Launch

App API explained in simple language for app developers

App API infographic