-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from laruby2/twilio-register
Use Twilio text messages for user register (US only for now)
- Loading branch information
Showing
13 changed files
with
197 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,3 +73,4 @@ group :test do | |
end | ||
|
||
gem "tailwindcss-rails", "= 2.0.23" | ||
gem "twilio-ruby", "~> 7.2" |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
class PhoneNumbersController < ApplicationController | ||
|
||
# post request to twilio | ||
def verify | ||
telephone = params[:phone_number] | ||
|
||
r = /\A\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})\z/ | ||
m = r.match(params[:phone_number]) | ||
if m.present? && m.length == 4 | ||
telephone = "+1#{m[1]}#{m[2]}#{m[3]}" # E.164 format | ||
end | ||
|
||
response = client.lookups.v1.phone_numbers(telephone).fetch | ||
valid_num = response.phone_number # if invalid, throws an exception | ||
|
||
verification = client | ||
.verify | ||
.v2 | ||
.services(service_sid) | ||
.verifications | ||
.create( | ||
to: valid_num, | ||
channel: 'sms' | ||
) | ||
|
||
# go to passcode form after posting twilio | ||
if verification.status == 'pending' | ||
session[:telephone] = telephone | ||
redirect_to phone_numbers_passcode_url | ||
else | ||
flash.now[:alert] = "Verification did not process" | ||
render :new, status: :unprocessable_entity | ||
end | ||
rescue => e | ||
if e.code == 20404 | ||
flash.now[:alert] = "Not a valid phone number" | ||
render :new, status: :unprocessable_entity | ||
else | ||
raise e | ||
end | ||
end | ||
|
||
def passcode_enter | ||
verification_check = client | ||
.verify | ||
.v2 | ||
.services(service_sid) | ||
.verification_checks | ||
.create( | ||
to: session[:telephone], | ||
code: params[:passcode] | ||
) | ||
|
||
# go back to user siginup page when successful | ||
if verification_check.status == 'approved' | ||
|
||
redirect_to register_url, notice: "Phone number is verified" | ||
else | ||
flash.now[:alert] = "Passcode is not being approved" | ||
render :edit, status: :unprocessable_entity | ||
end | ||
rescue => e | ||
if e.code == 20404 | ||
flash.now[:alert] = "Not a valid passcode" | ||
render :edit, status: :unprocessable_entity | ||
else | ||
raise e | ||
end | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def new | ||
end | ||
|
||
private | ||
|
||
def client | ||
@client ||= Twilio::REST::Client.new(account_sid, auth_token) | ||
end | ||
|
||
def account_sid | ||
ENV['TWILIO_ACCOUNT_SID'] | ||
end | ||
|
||
def auth_token | ||
ENV['TWILIO_AUTH_TOKEN'] | ||
end | ||
|
||
def service_sid | ||
ENV['TWILIO_SERVICE_SID'] | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ def create | |
|
||
def destroy | ||
session[:user_id] = nil | ||
session[:telephone] = nil | ||
redirect_to new_session_url | ||
end | ||
end |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<div class="mb-6 px-6 max-w-3xl mx-auto lg:ml-0 lg:mr-auto xl:mx-0 xl:px-12 xl:w-3/4"> | ||
<h2 class="text-3xl font-bold text-gray-800">Enter 6 digit passcode</h2> | ||
<hr class="my-8 border-b-2 border-gray-200"> | ||
</div> | ||
|
||
<%= form_with url: phone_numbers_passcode_path, class: "w-full max-w-sm" do |f| %> | ||
<div class="md:flex md:items-center mb-6"> | ||
<div class="md:w-1/3"> | ||
<%= f.label :passcode, class: "block text-gray-500 font-bold md:text-right mb-1 md:mb-0 pr-4" %> | ||
</div> | ||
<div class="md:w-2/3"> | ||
<%= f.password_field :passcode, required: true, class: "bg-gray-200 appearance-none border-2 border-gray-200 rounded w-full py-2 px-4 text-gray-700 leading-tight focus:outline-none focus:bg-white focus:border-purple-500" %> | ||
</div> | ||
</div> | ||
<div class="md:flex md:items-center"> | ||
<div class="md:w-1/3"></div> | ||
<div class="md:w-2/3"> | ||
<%= f.submit "Submit", class: "shadow bg-purple-500 hover:bg-purple-400 focus:shadow-outline focus:outline-none text-white font-bold py-2 px-4 rounded" %> | ||
<%= link_to "Cancel", root_path, class: "py-2 px-4 border border-gray-500 rounded text-gray-500 hover:bg-gray-100 hover:border-gray-700 hover:text-gray-700 font-bold" %> | ||
</div> | ||
</div> | ||
<% end %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<div class="mb-6 px-6 max-w-3xl mx-auto lg:ml-0 lg:mr-auto xl:mx-0 xl:px-12 xl:w-3/4"> | ||
<h2 class="text-3xl font-bold text-gray-800">Verify phone number</h2> | ||
<hr class="my-8 border-b-2 border-gray-200"> | ||
|
||
<p class="text-gray-400"> | ||
Do you have a username already? Then | ||
<%= link_to "Sign In", signin_path, class: "text-green-500 underline" %> | ||
</p> | ||
</div> | ||
|
||
<%= form_with model: @user, class: "w-full max-w-sm" do |f| %> | ||
|
||
<div class="md:flex md:items-center mb-6"> | ||
<div class="md:w-1/3"> | ||
<%= f.label :phone_number, class: "block text-gray-500 font-bold md:text-right mb-1 md:mb-0 pr-4" %> | ||
</div> | ||
<div class="md:w-2/3"> | ||
<%= f.phone_field :phone_number, autofocus: true, required: true, class: "bg-gray-200 appearance-none border-2 border-gray-200 rounded w-full py-2 px-4 text-gray-700 leading-tight focus:outline-none focus:bg-white focus:border-purple-500" %> | ||
</div> | ||
</div> | ||
|
||
<div class="md:flex md:items-center"> | ||
<div class="md:w-1/3"></div> | ||
<div class="md:w-2/3"> | ||
<%= f.submit "Verify", formaction: phone_numbers_verify_path, class: "shadow bg-purple-500 hover:bg-purple-400 focus:shadow-outline focus:outline-none text-white font-bold py-2 px-4 rounded" %> | ||
</div> | ||
</div> | ||
<% end %> |
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
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
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
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