Created
January 11, 2018 23:56
-
-
Save daydream05/f3168f173264dcc1ae1f2bc7585104ba to your computer and use it in GitHub Desktop.
don't use for production. for tutorial purposes only.
This file contains 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
class Api::V1::PhoneVerificationsController < ApplicationController | |
def start | |
response = Authy::PhoneVerification.start(via: "sms", country_code: 1, phone_number: phone_number_params) | |
if response.ok? | |
# verification was started | |
render json: {"success": true, message: "Verification code was sent to your phone number"}, status: :ok | |
else | |
render json: response | |
end | |
end | |
def check | |
phone_number = phone_verification_params[:phone_number] | |
verification_code = phone_verification_params[:verification_code] | |
response = Authy::PhoneVerification.check(verification_code: verification_code, country_code: 1, | |
phone_number: phone_number) | |
if response.ok? | |
# verification code was accepted | |
# For tutorial purposes, we're going to update by id. | |
# Use current_user or whichever helper method your authentication provides | |
# so your user won't update some random person's phone number. | |
user = User.find_by_id(params[:user_id]) | |
user.update(phone_number_params) | |
render json: {"success": true, "message": "Phone number was succesfully verified"}, | |
status: 201 | |
else | |
render json: response | |
end | |
end | |
private | |
def phone_verification_params | |
params.require(:phone_verification).permit(:phone_number, :verification_code) | |
end | |
def phone_number_params | |
params.require(:phone_verification).permit(:phone_number) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment