Created
August 19, 2020 07:28
-
-
Save kennethteh90/f4ef538c4db76edc8bf653fd63980530 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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