devise_phone 0.0.16 → 0.0.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -0
- data/README.rdoc +55 -31
- data/app/controllers/devise/phone_verifications_controller.rb +27 -34
- data/app/views/devise/phone/_activate_phone.html.erb +7 -0
- data/app/views/devise/phone/_resend_code.html.erb +6 -0
- data/config/locales/en.yml +11 -11
- data/lib/devise_phone.rb +0 -16
- data/lib/devise_phone/controllers/helpers.rb +0 -9
- data/lib/devise_phone/routes.rb +3 -3
- data/lib/devise_phone/version.rb +1 -1
- data/lib/generators/devise_phone/install_generator.rb +2 -31
- data/lib/generators/devise_phone/views_generator.rb +12 -1
- data/lib/models/phone.rb +76 -177
- metadata +5 -6
- data/app/views/devise/phone/insert.html.erb +0 -11
- data/app/views/devise/phone/new.html.erb +0 -12
- data/lib/generators/templates/lib/sms_sender.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 247614aea59c90856236ad68b9e082a51bee8e7f
|
4
|
+
data.tar.gz: eb4f3309cbe2481c05161005194083e5c9293f07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c41bfd45c319e8b3053e8da035fc69f48707fdfd83686f9ea8095c2ccab627853a73077462410e57d3eb64c28bddf79a34f6b15c7d5290fc7081be18ce029269
|
7
|
+
data.tar.gz: 2208150b39a3810ec53145976a274aa865c66d450e2e88abd21aa3971a3b5cd9e78b3521dad203334e7188d154a1ff36b3996c23535ddbbb2ee11405774f7fd0
|
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -1,40 +1,54 @@
|
|
1
|
-
=
|
1
|
+
= devise_phone
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Ask the user his phone (and phone confirmation to double check) on registration and the token will be sended automagically.
|
6
|
-
|
7
|
-
== Installation for Rails >= 3.0 and Devise >= 1.1
|
8
|
-
|
9
|
-
Install DevisePhone gem, it will also install dependencies (such as devise and warden):
|
10
|
-
|
11
|
-
gem install devise_phone
|
3
|
+
The user will receive an SMS with a token that can be entered on the site to activate the phone number.
|
4
|
+
Ask the user his phone and the token will be sended automagically.
|
12
5
|
|
13
6
|
Add DevisePhone to your Gemfile (and Devise and TwilioRuby if you weren't using them):
|
14
7
|
|
15
|
-
gem 'devise'
|
16
|
-
gem 'twilio-ruby'
|
17
|
-
gem 'devise_phone'
|
8
|
+
gem 'devise'
|
9
|
+
gem 'twilio-ruby'
|
10
|
+
gem 'devise_phone'
|
18
11
|
|
19
12
|
=== Automatic installation
|
20
13
|
|
14
|
+
Run:
|
15
|
+
rails generate devise:install
|
16
|
+
rails generate devise MODEL
|
17
|
+
rails generate devise:views MODEL
|
18
|
+
before installing devise_phone in your Rails app
|
19
|
+
|
21
20
|
Run the following generator to add DevisePhone’s configuration option in the Devise configuration file (config/initializers/devise.rb) and the sms sender class in your lib folder:
|
22
21
|
|
23
22
|
rails generate devise_phone:install
|
24
23
|
|
25
|
-
When you are done, you are ready to add DevisePhone to any of your Devise models using the following generator:
|
24
|
+
When you are done, you are ready to add DevisePhone to any of your Devise models using the following generator:
|
26
25
|
|
27
26
|
rails generate devise_phone MODEL
|
28
27
|
|
29
28
|
Replace MODEL by the class name you want to add DevisePhone, like User, Admin, etc. This will add the :phone flag to your model's Devise modules. The generator will also create a migration file (if your ORM support them). Continue reading this file to understand exactly what the generator produces and how to use it.
|
30
29
|
|
30
|
+
In your config/environments, please specify your twilio_sid, twilio_token, and twilio_phone_number:
|
31
|
+
|
32
|
+
config.twilio = {
|
33
|
+
sid: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
|
34
|
+
token: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
|
35
|
+
phone_number: '123455678',
|
36
|
+
}
|
37
|
+
|
38
|
+
To specify the message body, in devise.en.yml add:
|
39
|
+
|
40
|
+
en:
|
41
|
+
devise:
|
42
|
+
phone:
|
43
|
+
message_body: "Hi! This is Company Name. Your verification code is %{verification_code}."
|
44
|
+
|
31
45
|
== Configuring views
|
32
46
|
|
33
47
|
All the views are packaged inside the gem. If you'd like to customize the views, invoke the following generator and it will copy all the views to your application:
|
34
48
|
|
35
49
|
rails generate devise_phone:views
|
36
50
|
|
37
|
-
You can also use the generator to generate scoped views:
|
51
|
+
You can also use the generator to generate scoped views: (This might not work yet)
|
38
52
|
|
39
53
|
rails generate devise_phone:views users
|
40
54
|
|
@@ -42,30 +56,40 @@ Please refer to {Devise's README}[http://github.com/plataformatec/devise] for mo
|
|
42
56
|
|
43
57
|
== Usage
|
44
58
|
|
45
|
-
|
59
|
+
Don't forget to add phone_number as one of the permitted parameters. This is an example of doing so:
|
60
|
+
|
61
|
+
class ApplicationController < ActionController::Base
|
62
|
+
# Prevent CSRF attacks by raising an exception.
|
63
|
+
# For APIs, you may want to use :null_session instead.
|
64
|
+
protect_from_forgery with: :exception
|
65
|
+
|
66
|
+
before_filter :configure_permitted_parameters
|
46
67
|
|
47
|
-
|
48
|
-
By default users MUST activate by SMS before entering.
|
49
|
-
If you want something more "relaxed" just override <tt>sms_confirmation_required?</tt> in your model and make it your way.
|
50
|
-
You can use the convenience filter <tt>require_sms_activated!</tt> in your controller to block sms-unactive users from specific pages.
|
68
|
+
protected
|
51
69
|
|
52
|
-
|
70
|
+
# my custom fields are :name, :heard_how
|
71
|
+
def configure_permitted_parameters
|
72
|
+
devise_parameter_sanitizer.for(:sign_up) do |u|
|
73
|
+
u.permit(:phone_number,
|
74
|
+
:email, :password, :password_confirmation)
|
75
|
+
end
|
76
|
+
devise_parameter_sanitizer.for(:account_update) do |u|
|
77
|
+
u.permit(:phone_number,
|
78
|
+
:email, :password, :password_confirmation, :current_password)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
53
82
|
|
54
|
-
|
83
|
+
The resend verification code button can be inserted to any page just by using:
|
84
|
+
<%= render 'devise/phone/resend_code' %>
|
55
85
|
|
56
|
-
|
86
|
+
The activate phone field and button can be inserted to any page just by using:
|
87
|
+
<%= render 'devise/phone/activate_phone' %>
|
57
88
|
|
58
|
-
DevisePhone installs a localizable file in your config/locales folder.
|
59
89
|
|
60
90
|
== Contributing to devise_phone
|
61
91
|
|
62
|
-
*
|
63
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
64
|
-
* Fork the project
|
65
|
-
* Start a feature/bugfix branch
|
66
|
-
* Commit and push until you are happy with your contribution
|
67
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
68
|
-
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
92
|
+
* email htheodore@gmail.com
|
69
93
|
|
70
94
|
== Copyright
|
71
95
|
|
@@ -1,45 +1,38 @@
|
|
1
1
|
class Devise::PhoneVerificationsController < DeviseController
|
2
2
|
|
3
3
|
# GET /resource/phone_verification/new
|
4
|
-
def new
|
5
|
-
|
6
|
-
|
7
|
-
end
|
4
|
+
# def new
|
5
|
+
# build_resource({})
|
6
|
+
# render :new
|
7
|
+
# end
|
8
8
|
|
9
9
|
# POST /resource/phone_verification
|
10
|
-
def create
|
11
|
-
|
12
|
-
self.send_verification_code
|
13
|
-
|
14
|
-
# self.resource = resource_class.send_verification_code
|
15
|
-
|
16
|
-
# if resource.errors.empty?
|
17
|
-
# set_flash_message :notice, :send_token, :phone => self.resource.phone
|
18
|
-
# redirect_to new_session_path(resource_name)
|
19
|
-
# else
|
20
|
-
# render :new
|
21
|
-
# end
|
22
|
-
end
|
10
|
+
# def create
|
11
|
+
# end
|
23
12
|
|
24
|
-
# GET /resource/phone_verification/
|
25
|
-
def
|
26
|
-
|
13
|
+
# GET /resource/phone_verification/send_code
|
14
|
+
def send_code
|
15
|
+
current_user.generate_verification_code_and_send_sms
|
16
|
+
# render nothing: true
|
17
|
+
respond_to do |format|
|
18
|
+
msg = { :status => "ok", :message => "SMS sent!" }
|
19
|
+
format.json { render :json => msg } # don't do msg.to_json
|
20
|
+
end
|
27
21
|
end
|
28
22
|
|
29
|
-
# GET or POST /resource/phone_verification/
|
30
|
-
def
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
23
|
+
# GET or POST /resource/phone_verification/verify_code
|
24
|
+
def verify_code
|
25
|
+
verify_success = current_user.verify_phone_number_with_code_entered(params[:code_entered])
|
26
|
+
# render nothing: true
|
27
|
+
respond_to do |format|
|
28
|
+
if verify_success
|
29
|
+
message_response = "verification successful"
|
30
|
+
else
|
31
|
+
message_response = "verification fail"
|
32
|
+
end
|
33
|
+
msg = { :status => "ok", :message => message_response }
|
34
|
+
format.json { render :json => msg } # don't do msg.to_json
|
35
|
+
end
|
43
36
|
end
|
44
37
|
|
45
38
|
protected
|
@@ -0,0 +1,7 @@
|
|
1
|
+
<%= form_for(resource, :as => resource_name, :url => verify_code_user_phone_verification_path(resource_name), :html => { :method => :post }, remote: true) do |f| %>
|
2
|
+
|
3
|
+
<p><%=label_tag :code_entered %><br />
|
4
|
+
<%=text_field_tag :code_entered, "" %></p>
|
5
|
+
|
6
|
+
<p><%= f.submit "Activate" %></p>
|
7
|
+
<% end %>
|
data/config/locales/en.yml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
en:
|
2
2
|
errors:
|
3
3
|
messages:
|
4
|
-
|
5
|
-
|
6
|
-
sms_token_invalid: "was not locked"
|
7
|
-
devise:
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
4
|
+
empty_phone_number_field: "Phone number field is empty"
|
5
|
+
phone_verification_not_needed: "Phone number is already verified"
|
6
|
+
# sms_token_invalid: "was not locked"
|
7
|
+
# devise:
|
8
|
+
# sms_activations:
|
9
|
+
# send_token: 'An activation token was sent by SMS to %{phone}.'
|
10
|
+
# sms_token_invalid: 'The sms token provided is not valid!'
|
11
|
+
# confirmed: 'Your account has been activated. You are now signed in.'
|
12
|
+
# sms_activation_required: 'SMS Activation is required'
|
13
|
+
# sms_body: 'Your Activation Token is %{sms_confirmation_token}.'
|
14
|
+
# unconfirmed_sms: 'Your account need to be activated with an SMS token'
|
15
15
|
|
data/lib/devise_phone.rb
CHANGED
@@ -10,22 +10,6 @@ require 'devise_phone/controllers/helpers'
|
|
10
10
|
require 'devise_phone/rails'
|
11
11
|
|
12
12
|
module Devise
|
13
|
-
# mattr_accessor :sms_confirm_within
|
14
|
-
# @@sms_confirm_within = 2.days
|
15
|
-
# mattr_accessor :sms_confirmation_keys
|
16
|
-
# @@sms_confirmation_keys = [:email]
|
17
|
-
|
18
|
-
# Get the sms sender class from the mailer reference object.
|
19
|
-
def self.sms_sender
|
20
|
-
@@sms_sender_ref.get
|
21
|
-
end
|
22
|
-
|
23
|
-
# Set the smser reference object to access the smser.
|
24
|
-
def self.sms_sender=(class_name)
|
25
|
-
@@sms_sender_ref = ActiveSupport::Dependencies.reference(class_name)
|
26
|
-
end
|
27
|
-
|
28
|
-
self.sms_sender = "Devise::SmsSender"
|
29
13
|
end
|
30
14
|
|
31
15
|
Devise.add_module :phone, :model => "models/phone", :controller => :phone_verifications, :route => :phone_verification
|
@@ -1,12 +1,3 @@
|
|
1
1
|
module DevisePhone::Controllers::Helpers
|
2
|
-
protected
|
3
|
-
|
4
|
-
# Convenience helper to check if user has confirmed the token (and the phone) or not.
|
5
|
-
def require_sms_activated!
|
6
|
-
if(send(:"authenticate_#{resource_name}!"))
|
7
|
-
res=send(:"current_#{resource_name}")
|
8
|
-
fail!(:sms_activation_required) if (!res) || (!res.sms_confirmed?)
|
9
|
-
end
|
10
|
-
end
|
11
2
|
end
|
12
3
|
ActionController::Base.send :include, DevisePhone::Controllers::Helpers
|
data/lib/devise_phone/routes.rb
CHANGED
@@ -2,10 +2,10 @@ module ActionDispatch::Routing
|
|
2
2
|
class Mapper
|
3
3
|
|
4
4
|
protected
|
5
|
-
def
|
5
|
+
def devise_phone_verification(mapping, controllers)
|
6
6
|
resource :phone_verification, :only => [:new, :create], :path => mapping.path_names[:phone_verification], :controller => controllers[:phone_verifications] do
|
7
|
-
|
8
|
-
get :
|
7
|
+
post :verify_code, :path => mapping.path_names[:verify_code], :as => :verify_code
|
8
|
+
get :send_code, :path => mapping.path_names[:send_code], :as => :send_code
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
data/lib/devise_phone/version.rb
CHANGED
@@ -16,40 +16,11 @@ module DevisePhone
|
|
16
16
|
if old_content.match(Regexp.new(/^\s# ==> Configuration for :phone\n/))
|
17
17
|
false
|
18
18
|
end
|
19
|
+
|
19
20
|
end
|
20
|
-
end
|
21
|
-
# else
|
22
|
-
# inject_into_file(devise_initializer_path, :before => " # ==> Configuration for :confirmable\n") do
|
23
|
-
# <<-CONTENT
|
24
|
-
# # ==> Configuration for :phone
|
25
|
-
# # The period the generated sms token is valid, after
|
26
|
-
# # this period, the user won't be able to activate.
|
27
|
-
# # config.sms_confirm_within = 0.days
|
28
21
|
|
29
|
-
# # The keys searched for confirmation values.
|
30
|
-
# # config.sms_confirmation_keys = [:email]
|
31
|
-
|
32
|
-
# # Your SmsSender class. The provided one uses
|
33
|
-
# # moonshado-sms gem so install it and configure
|
34
|
-
# # if you want to use it.
|
35
|
-
# # A simple instance of the class has been copied in your lib folder
|
36
|
-
# # For further informations on using and configuring moonshado-sms gem check
|
37
|
-
# # https://github.com/moonshado/moonshado-sms
|
38
|
-
# # config.sms_sender = "Devise::SmsSender"
|
39
|
-
|
40
|
-
# CONTENT
|
41
|
-
# end
|
42
|
-
# end
|
43
|
-
# end
|
44
|
-
# end
|
45
|
-
|
46
|
-
# def copy_locale
|
47
|
-
# copy_file "../../../config/locales/en.yml", "config/locales/devise_phone.en.yml"
|
48
|
-
# end
|
49
|
-
|
50
|
-
def copy_default_smser
|
51
|
-
copy_file "lib/sms_sender.rb", "lib/devise_sms_sender.rb"
|
52
22
|
end
|
23
|
+
|
53
24
|
end
|
54
25
|
end
|
55
26
|
end
|
@@ -3,8 +3,19 @@ require 'generators/devise/views_generator'
|
|
3
3
|
module DevisePhone
|
4
4
|
module Generators
|
5
5
|
class ViewsGenerator < Devise::Generators::ViewsGenerator
|
6
|
-
source_root File.expand_path("../../../../app/views", __FILE__)
|
6
|
+
source_root File.expand_path("../../../../app/views/devise", __FILE__)
|
7
7
|
desc 'Copies all DevisePhone views to your application.'
|
8
|
+
|
9
|
+
def generate_view
|
10
|
+
directory 'phone', "#{target_path}/phone"
|
11
|
+
# directory 'path_to_install_directory', 'path_to_source_directory'
|
12
|
+
end
|
13
|
+
|
14
|
+
def target_path
|
15
|
+
@target_path ||= "app/views/#{scope || :devise}"
|
16
|
+
end
|
17
|
+
|
18
|
+
|
8
19
|
end
|
9
20
|
end
|
10
21
|
end
|
data/lib/models/phone.rb
CHANGED
@@ -2,214 +2,113 @@ require "devise_phone/hooks"
|
|
2
2
|
|
3
3
|
module Devise
|
4
4
|
module Models
|
5
|
-
# SmsActivable is responsible to verify if an account is already confirmed to
|
6
|
-
# sign in, and to send sms with confirmation instructions.
|
7
|
-
# Confirmation instructions are sent to the user phone after creating a
|
8
|
-
# record and when manually requested by a new confirmation instruction request.
|
9
|
-
#
|
10
|
-
# == Options
|
11
|
-
#
|
12
|
-
# Confirmable adds the following options to devise_for:
|
13
|
-
#
|
14
|
-
# * +sms_confirm_within+: the time you want to allow the user to access his account
|
15
|
-
# before confirming it. After this period, the user access is denied. You can
|
16
|
-
# use this to let your user access some features of your application without
|
17
|
-
# confirming the account, but blocking it after a certain period (ie 7 days).
|
18
|
-
# By default confirm_within is 0 days, so the user must confirm before entering.
|
19
|
-
# If you want to allow user to use parts of the site and block others override
|
20
|
-
# sms_confirmation_required? and check manually on selected pages using the
|
21
|
-
# require_sms_activated! helper or sms_confirmed? property on record
|
22
|
-
#
|
23
|
-
# == Examples
|
24
|
-
#
|
25
|
-
# User.find(1).sms_confirm! # returns true unless it's already confirmed
|
26
|
-
# User.find(1).sms_confirmed? # true/false
|
27
|
-
# User.find(1).send_sms_token # manually send token
|
28
|
-
#
|
29
5
|
module Phone
|
30
6
|
extend ActiveSupport::Concern
|
31
7
|
|
32
8
|
included do
|
33
|
-
before_create :
|
34
|
-
after_create :
|
9
|
+
before_create :set_unverified_phone_attributes, :if => :phone_verification_needed?
|
10
|
+
# after_create :private_generate_verification_code_and_send_sms, :if => :phone_verification_needed?
|
11
|
+
# before_save :remember_old_phone_number
|
12
|
+
after_save :private_generate_verification_code_and_send_sms, :if => :regenerate_phone_verification_needed?
|
35
13
|
end
|
36
14
|
|
37
|
-
# # Confirm a user by setting it's sms_confirmed_at to actual time. If the user
|
38
|
-
# # is already confirmed, add en error to email field
|
39
|
-
# def confirm_sms!
|
40
|
-
# unless_sms_confirmed do
|
41
|
-
# self.sms_confirmation_token = nil
|
42
|
-
# self.sms_confirmed_at = Time.now
|
43
|
-
# save(:validate => false)
|
44
|
-
# end
|
45
|
-
# end
|
46
|
-
|
47
|
-
# # Verifies whether a user is sms-confirmed or not
|
48
|
-
# def confirmed_sms?
|
49
|
-
# !!sms_confirmed_at
|
50
|
-
# end
|
51
|
-
|
52
|
-
# Send confirmation token by sms
|
53
15
|
def generate_verification_code_and_send_sms
|
54
|
-
|
55
|
-
|
56
|
-
|
16
|
+
if(phone_verification_needed?)
|
17
|
+
private_generate_verification_code_and_send_sms
|
18
|
+
end
|
19
|
+
self.save!
|
20
|
+
end
|
21
|
+
|
22
|
+
def verify_phone_number_with_code_entered(code_entered)
|
23
|
+
if phone_verification_needed? && (code_entered == self.phone_verification_code)
|
24
|
+
mark_phone_as_verified!
|
25
|
+
true
|
57
26
|
else
|
58
|
-
# self.errors.add(:sms_confirmation_token, :no_phone_associated)
|
59
27
|
false
|
60
28
|
end
|
61
29
|
end
|
62
30
|
|
63
|
-
|
64
|
-
# def resend_sms_token
|
65
|
-
# unless_sms_confirmed { send_sms_token }
|
66
|
-
# end
|
31
|
+
private
|
67
32
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
# end
|
76
|
-
|
77
|
-
# # The message to be shown if the account is inactive.
|
78
|
-
# def inactive_message
|
79
|
-
# !confirmed_sms? ? I18n.t(:"devise.sms_activations.unconfirmed_sms") : super
|
80
|
-
# end
|
33
|
+
def private_generate_verification_code_and_send_sms
|
34
|
+
self.phone_verification_code = generate_phone_verification_code
|
35
|
+
set_unverified_phone_attributes
|
36
|
+
if phone_number.present?
|
37
|
+
send_sms_verification_code
|
38
|
+
end
|
39
|
+
end
|
81
40
|
|
82
|
-
# # If you don't want confirmation to be sent on create, neither a code
|
83
|
-
# # to be generated, call skip_sms_confirmation!
|
84
|
-
# def skip_sms_confirmation!
|
85
|
-
# self.sms_confirmed_at = Time.now
|
86
|
-
# end
|
87
41
|
|
88
|
-
|
42
|
+
def mark_phone_as_verified!
|
43
|
+
update!(phone_number_verified: true,
|
44
|
+
phone_verification_code: nil,
|
45
|
+
phone_verification_code_sent_at: nil,
|
46
|
+
phone_verified_at: DateTime.now)
|
47
|
+
end
|
89
48
|
|
90
|
-
#
|
49
|
+
# check if phone verification is needed and set errors here
|
91
50
|
def phone_verification_needed?
|
92
|
-
phone_number.
|
51
|
+
if phone_number.blank?
|
52
|
+
self.errors.add(:phone_verification_code, :empty_phone_number_field)
|
53
|
+
false
|
54
|
+
elsif phone_number_verified
|
55
|
+
self.errors.add(:phone_verification_code, :phone_verification_not_needed)
|
56
|
+
false
|
57
|
+
else
|
58
|
+
true
|
59
|
+
end
|
93
60
|
end
|
94
61
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
#
|
109
|
-
# # sms_confirm_within = 5.days and sms_confirmation_sent_at = 4.days.ago
|
110
|
-
# confirmation_period_valid? # returns true
|
111
|
-
#
|
112
|
-
# # sms_confirm_within = 5.days and sms_confirmation_sent_at = 5.days.ago
|
113
|
-
# confirmation_period_valid? # returns false
|
114
|
-
#
|
115
|
-
# # sms_confirm_within = 0.days
|
116
|
-
# confirmation_period_valid? # will always return false
|
117
|
-
#
|
118
|
-
# def confirmation_sms_period_valid?
|
119
|
-
# sms_confirmation_sent_at && sms_confirmation_sent_at.utc >= self.class.sms_confirm_within.ago
|
120
|
-
# end
|
121
|
-
|
122
|
-
# # Checks whether the record is confirmed or not, yielding to the block
|
123
|
-
# # if it's already confirmed, otherwise adds an error to email.
|
124
|
-
# def unless_sms_confirmed
|
125
|
-
# unless confirmed_sms?
|
126
|
-
# yield
|
127
|
-
# else
|
128
|
-
# self.errors.add(:sms_confirmation_token, :sms_already_confirmed)
|
129
|
-
# false
|
130
|
-
# end
|
131
|
-
# end
|
132
|
-
|
133
|
-
# Generates a new random token for confirmation, and stores the time
|
134
|
-
# this token is being generated
|
135
|
-
def set_phone_attributes
|
62
|
+
def regenerate_phone_verification_needed?
|
63
|
+
if phone_number.present?
|
64
|
+
if phone_number_changed?
|
65
|
+
true
|
66
|
+
else
|
67
|
+
false
|
68
|
+
end
|
69
|
+
# self.errors.add(:phone_verification_code, :empty_phone_number_field)
|
70
|
+
# false
|
71
|
+
else
|
72
|
+
false
|
73
|
+
end
|
74
|
+
end
|
136
75
|
|
76
|
+
# set attributes to user indicating the phone number is unverified
|
77
|
+
def set_unverified_phone_attributes
|
137
78
|
self.phone_number_verified = false
|
138
79
|
self.phone_verification_code_sent_at = DateTime.now
|
139
80
|
self.phone_verified_at = nil
|
140
81
|
# removes all white spaces, hyphens, and parenthesis
|
141
|
-
self.phone_number
|
82
|
+
if self.phone_number
|
83
|
+
self.phone_number.gsub!(/[\s\-\(\)]+/, '')
|
84
|
+
end
|
142
85
|
end
|
143
86
|
|
87
|
+
# return 4 digits random code 0-9
|
144
88
|
def generate_phone_verification_code
|
145
|
-
|
146
|
-
verification_code = SecureRandom.hex(3)
|
147
|
-
# end while self.class.exists?(phone_verification_code: verification_code)
|
89
|
+
verification_code = SecureRandom.random_number(10 ** 4).to_s.rjust(4,'0')
|
148
90
|
verification_code
|
149
91
|
end
|
150
92
|
|
151
|
-
#
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
# # If the user is already confirmed, create an error for the user
|
169
|
-
# # Options must have the sms_confirmation_token
|
170
|
-
# def confirm_by_sms_token(sms_confirmation_token)
|
171
|
-
# sms_confirmable = find_or_initialize_with_error_by(:sms_confirmation_token, sms_confirmation_token)
|
172
|
-
# sms_confirmable.confirm_sms! if sms_confirmable.persisted?
|
173
|
-
# sms_confirmable
|
174
|
-
# end
|
175
|
-
|
176
|
-
def mark_phone_as_verified!
|
177
|
-
update!(phone_number_verified: true,
|
178
|
-
phone_verification_code: nil,
|
179
|
-
phone_verification_code_sent_at: nil,
|
180
|
-
phone_verified_at: DateTime.now)
|
181
|
-
end
|
182
|
-
|
183
|
-
def verify_phone_number_with_code_entered(code_entered)
|
184
|
-
if self.phone_verification_code == code_entered
|
185
|
-
mark_phone_as_verified!
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
def send_verification_code
|
190
|
-
self.set_phone_attributes
|
191
|
-
if self.save!
|
192
|
-
send_sms_for_phone_verification
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
# # Generates a small token that can be used conveniently on SMS's.
|
197
|
-
# # The token is 5 chars long and uppercased.
|
198
|
-
|
199
|
-
# def generate_small_token(column)
|
200
|
-
# loop do
|
201
|
-
# token = Devise.friendly_token[0,5].upcase
|
202
|
-
# break token unless to_adapter.find_first({ column => token })
|
203
|
-
# end
|
204
|
-
# end
|
205
|
-
|
206
|
-
# # Generate an sms token checking if one does not already exist in the database.
|
207
|
-
# def sms_confirmation_token
|
208
|
-
# generate_small_token(:sms_confirmation_token)
|
209
|
-
# end
|
210
|
-
|
211
|
-
# Devise::Models.config(self, :sms_confirm_within, :sms_confirmation_keys)
|
93
|
+
# sends a message to number indicated in the secrets.yml
|
94
|
+
def send_sms_verification_code
|
95
|
+
number_to_send_to = self.phone_number
|
96
|
+
verification_code = self.phone_verification_code
|
97
|
+
|
98
|
+
twilio_sid = Rails.application.config.twilio[:sid]
|
99
|
+
twilio_token = Rails.application.config.twilio[:token]
|
100
|
+
twilio_phone_number = Rails.application.config.twilio[:phone_number]
|
101
|
+
twilio_message_body = I18n.t("devise.phone.message_body", :verification_code => verification_code)
|
102
|
+
|
103
|
+
@twilio_client = Twilio::REST::Client.new twilio_sid, twilio_token
|
104
|
+
|
105
|
+
@twilio_client.account.messages.create(
|
106
|
+
:from => "+1#{twilio_phone_number}",
|
107
|
+
:to => number_to_send_to,
|
108
|
+
:body => twilio_message_body
|
109
|
+
)
|
212
110
|
end
|
111
|
+
|
213
112
|
end
|
214
113
|
end
|
215
114
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devise_phone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.17
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hubert Theodore
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -96,8 +96,8 @@ files:
|
|
96
96
|
- README.rdoc
|
97
97
|
- Rakefile
|
98
98
|
- app/controllers/devise/phone_verifications_controller.rb
|
99
|
-
- app/views/devise/phone/
|
100
|
-
- app/views/devise/phone/
|
99
|
+
- app/views/devise/phone/_activate_phone.html.erb
|
100
|
+
- app/views/devise/phone/_resend_code.html.erb
|
101
101
|
- config/locales/en.yml
|
102
102
|
- devise_phone.gemspec
|
103
103
|
- lib/devise_phone.rb
|
@@ -114,7 +114,6 @@ files:
|
|
114
114
|
- lib/generators/devise_phone/install_generator.rb
|
115
115
|
- lib/generators/devise_phone/views_generator.rb
|
116
116
|
- lib/generators/mongoid/devise_phone_generator.rb
|
117
|
-
- lib/generators/templates/lib/sms_sender.rb
|
118
117
|
- lib/models/phone.rb
|
119
118
|
- rails/init.rb
|
120
119
|
homepage: https://github.com/tjhubert/devise_phone
|
@@ -137,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
136
|
version: '0'
|
138
137
|
requirements: []
|
139
138
|
rubyforge_project:
|
140
|
-
rubygems_version: 2.4.
|
139
|
+
rubygems_version: 2.4.6
|
141
140
|
signing_key:
|
142
141
|
specification_version: 4
|
143
142
|
summary: Send SMS to verify phone number
|
@@ -1,11 +0,0 @@
|
|
1
|
-
<h2>Activate Phone Number</h2>
|
2
|
-
|
3
|
-
<%= form_for(resource, :as => resource_name, :url => consume_phone_verification_path(resource_name), :html => { :method => :post }) do |f| %>
|
4
|
-
|
5
|
-
<p><%=label_tag :code_entered %><br />
|
6
|
-
<%=text_field_tag :code_entered, "" %></p>
|
7
|
-
|
8
|
-
<p><%= f.submit "Activate" %></p>
|
9
|
-
<% end %>
|
10
|
-
|
11
|
-
<%= render :partial => "devise/shared/links" %>
|
@@ -1,12 +0,0 @@
|
|
1
|
-
<h2>Resend Phone Verification Code</h2>
|
2
|
-
|
3
|
-
<%= form_for(resource, :as => resource_name, :url => phone_verification_path(resource_name), :html => { :method => :post }) do |f| %>
|
4
|
-
<%= devise_error_messages! %>
|
5
|
-
|
6
|
-
<p><%= f.label :email %><br />
|
7
|
-
<%= f.email_field :email %></p>
|
8
|
-
|
9
|
-
<p><%= f.submit "Resend Phone Verification Code" %></p>
|
10
|
-
<% end %>
|
11
|
-
|
12
|
-
<%= render :partial => "devise/shared/links" %>
|
@@ -1,19 +0,0 @@
|
|
1
|
-
class Devise::SmsSender
|
2
|
-
#Actually sends the sms token. feel free to modify and adapt to your provider and/or gem
|
3
|
-
def send_sms_verification_code_to(user)
|
4
|
-
number_to_send_to = user.phone_number
|
5
|
-
verification_code = user.phone_verification_code
|
6
|
-
|
7
|
-
twilio_sid = "ACd35391c08cde7926e2295d1812ada918"
|
8
|
-
twilio_token = "44d79a36adb3d54cc15711d94d149119"
|
9
|
-
twilio_phone_number = "6502810746"
|
10
|
-
|
11
|
-
@twilio_client = Twilio::REST::Client.new twilio_sid, twilio_token
|
12
|
-
|
13
|
-
@twilio_client.account.sms.messages.create(
|
14
|
-
:from => "+1#{twilio_phone_number}",
|
15
|
-
:to => number_to_send_to,
|
16
|
-
:body => "Hi! This is MathCrunch. Your verification code is #{verification_code}"
|
17
|
-
)
|
18
|
-
end
|
19
|
-
end
|