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 computers communicate with each other

How computers communicate with each other

IdentityServer4 Setup Token Service Admin UI | Tutorial 1

Introduction to Microsoft Teams & It’s Automation using Graph APIs

How much does it Cost to Develop and Maintain Flutter Mobile App?

Stray Of (Software) Development

Code Sharing with Bloc

Unity Web Requests: Downloading (and working with) JSON text

Creating Security Cameras in Unity!

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

BLE Remote Car — Part 1

Circuit diagram for the car

How to solve the SSL error for Redis 6 on Heroku?

IoT Hidden Menu: Amazing Hack & Features on Your Android

Memory Management : free()