adds sms verification
This commit is contained in:
62
app/controllers/verification/sms_controller.rb
Normal file
62
app/controllers/verification/sms_controller.rb
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
class Verification::SmsController < ApplicationController
|
||||||
|
before_action :authenticate_user!
|
||||||
|
before_action :verify_resident!
|
||||||
|
before_action :verify_attemps_left!, only: [:new, :create]
|
||||||
|
|
||||||
|
skip_authorization_check
|
||||||
|
|
||||||
|
def new
|
||||||
|
@sms = Sms.new(phone: params[:phone])
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
@sms = Sms.new(sms_params.merge(user: current_user))
|
||||||
|
if @sms.save
|
||||||
|
redirect_to edit_sms_path, notice: t('verification.sms.create.flash.success')
|
||||||
|
else
|
||||||
|
render :new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def edit
|
||||||
|
@sms = Sms.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@sms = Sms.new(sms_params.merge(user: current_user))
|
||||||
|
if @sms.verify?
|
||||||
|
current_user.update(confirmed_phone: current_user.unconfirmed_phone)
|
||||||
|
|
||||||
|
if VerifiedUser.phone?(current_user)
|
||||||
|
current_user.update(verified_at: Time.now)
|
||||||
|
end
|
||||||
|
|
||||||
|
redirect_to_next_path
|
||||||
|
else
|
||||||
|
@error = t('verification.sms.update.error')
|
||||||
|
render :edit
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def sms_params
|
||||||
|
params.require(:sms).permit(:phone, :confirmation_code)
|
||||||
|
end
|
||||||
|
|
||||||
|
def redirect_to_next_path
|
||||||
|
current_user.reload
|
||||||
|
if current_user.level_three_verified?
|
||||||
|
redirect_to account_path, notice: t('verification.sms.update.flash.level_three.success')
|
||||||
|
else
|
||||||
|
redirect_to new_letter_path, notice: t('verification.sms.update.flash.level_two.success')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def verify_attemps_left!
|
||||||
|
if current_user.sms_confirmation_tries >= 3
|
||||||
|
redirect_to account_path, notice: t('verification.sms.alert.verify_attemps_left')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
47
app/models/sms.rb
Normal file
47
app/models/sms.rb
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
class Sms
|
||||||
|
include ActiveModel::Model
|
||||||
|
|
||||||
|
attr_accessor :user, :phone, :confirmation_code
|
||||||
|
|
||||||
|
validates_presence_of :phone
|
||||||
|
validates :phone, length: { is: 9 }
|
||||||
|
validate :spanish_phone
|
||||||
|
validate :uniqness_phone
|
||||||
|
|
||||||
|
def spanish_phone
|
||||||
|
errors.add(:phone, :invalid) unless phone.start_with?('6', '7')
|
||||||
|
end
|
||||||
|
|
||||||
|
def uniqness_phone
|
||||||
|
errors.add(:phone, :taken) if User.where(confirmed_phone: phone).any?
|
||||||
|
end
|
||||||
|
|
||||||
|
def save
|
||||||
|
return false unless self.valid?
|
||||||
|
update_user_phone_information
|
||||||
|
send_sms
|
||||||
|
increase_sms_tries
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_user_phone_information
|
||||||
|
user.update(unconfirmed_phone: phone, sms_confirmation_code: four_digit_code)
|
||||||
|
end
|
||||||
|
|
||||||
|
def send_sms
|
||||||
|
SMSApi.new.sms_deliver(user.unconfirmed_phone, user.sms_confirmation_code)
|
||||||
|
end
|
||||||
|
|
||||||
|
def increase_sms_tries
|
||||||
|
user.update(sms_confirmation_tries: user.sms_confirmation_tries += 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def verify?
|
||||||
|
user.sms_confirmation_code == confirmation_code
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def four_digit_code
|
||||||
|
rand.to_s[2..5]
|
||||||
|
end
|
||||||
|
end
|
||||||
5
app/views/verification/sms/_form.html.erb
Normal file
5
app/views/verification/sms/_form.html.erb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
<%= form_for sms, url: sms_path do |f| %>
|
||||||
|
<%= render 'shared/errors', resource: sms %>
|
||||||
|
<%= f.hidden_field :phone %>
|
||||||
|
<%= f.submit t('verification.sms.form.submit_button') %>
|
||||||
|
<% end %>
|
||||||
16
app/views/verification/sms/edit.html.erb
Normal file
16
app/views/verification/sms/edit.html.erb
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<div class="row account">
|
||||||
|
<div class="small-12 column">
|
||||||
|
|
||||||
|
<h1 class="inline-block"><%= t('verification.sms.edit.title') %></h1>
|
||||||
|
|
||||||
|
<%= form_for @sms, url: sms_path, method: :put do |f| %>
|
||||||
|
<p><%= @error %></p>
|
||||||
|
<%= f.text_field :confirmation_code %>
|
||||||
|
|
||||||
|
<%= f.submit t('verification.sms.edit.submit_button') %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<%= t('verification.sms.edit.resend_sms_text') %>
|
||||||
|
<%= link_to t('verification.sms.edit.resend_sms_link'), verified_user_path %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
14
app/views/verification/sms/new.html.erb
Normal file
14
app/views/verification/sms/new.html.erb
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<div class="row account">
|
||||||
|
<div class="small-12 column">
|
||||||
|
|
||||||
|
<h1 class="inline-block"><%= t('verification.sms.new.title') %></h1>
|
||||||
|
|
||||||
|
<%= form_for @sms, url: sms_path do |f| %>
|
||||||
|
<%= render 'shared/errors', resource: @sms %>
|
||||||
|
|
||||||
|
<%= f.text_field :phone %>
|
||||||
|
|
||||||
|
<%= f.submit t('verification.sms.new.submit_button') %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
Reference in New Issue
Block a user