Skip to content

Instantly share code, notes, and snippets.

@kennethteh90
Created August 19, 2020 07:28
Show Gist options
  • Save kennethteh90/f4ef538c4db76edc8bf653fd63980530 to your computer and use it in GitHub Desktop.
Save kennethteh90/f4ef538c4db76edc8bf653fd63980530 to your computer and use it in GitHub Desktop.
module Otp
def send_otp
result = sms_client.sms.send(
from: 'Us',
to: "+65#{mobile_number}",
text: "Your one time password is #{otp}"
)
raise SomeError unless result.messages[0].status == '0'
end
def verify_otp(otp_to_verify)
otp == otp_to_verify
end
private
def otp
Rails.cache.fetch("otp/#{cache_key}", expires_in: 3.minutes) do
Array.new(6) { rand(10) }.join
end
end
def sms_client
@sms_client ||= Nexmo::Client.new(api_key: Rails.application.secrets.nexmo_api_key,
api_secret: Rails.application.secrets.nexmo_secret)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment