devise_phone 0.0.1661 → 0.0.1662
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +22 -0
- data/app/controllers/devise/phone_verifications_controller.rb +4 -34
- data/app/views/devise/phone/_activate_phone.html.erb +0 -2
- data/config/locales/en.yml +11 -11
- data/lib/devise_phone.rb +0 -48
- data/lib/devise_phone/version.rb +1 -1
- data/lib/generators/devise_phone/install_generator.rb +2 -31
- data/lib/models/phone.rb +76 -113
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aea3bed47404803f7cccb39604bfa1b177198381
|
4
|
+
data.tar.gz: 507f7f19fb93b700d03d9b1b5c818f4a9de5b365
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88aa689d5440ed65a222dbb48c9368e8effe606b64253de7f74eb72253fe52c033c8cf8aae75e0bf89c9606384b520ebff069b98300ba3c91fb44d292463772e
|
7
|
+
data.tar.gz: 006cba759729181bb1b1825258acca717def542d5c73f59ea3fe5fdae689f2e006190f533f9687006097e486110a1b51b0f6101ece86be089884b3d236413a74
|
data/README.rdoc
CHANGED
@@ -27,6 +27,28 @@ When you are done, you are ready to add DevisePhone to any of your Devise models
|
|
27
27
|
|
28
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.
|
29
29
|
|
30
|
+
In your secrets.yml, please specify your twilio_sid, twilio_token, and twilio_phone_number:
|
31
|
+
|
32
|
+
development:
|
33
|
+
secret_key_base: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
34
|
+
twilio_sid: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
35
|
+
twilio_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
36
|
+
twilio_phone_number: 12345678
|
37
|
+
|
38
|
+
test:
|
39
|
+
secret_key_base: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
40
|
+
twilio_sid: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
41
|
+
twilio_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
42
|
+
twilio_phone_number: 12345678
|
43
|
+
|
44
|
+
# Do not keep production secrets in the repository,
|
45
|
+
# instead read values from the environment.
|
46
|
+
production:
|
47
|
+
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
|
48
|
+
twilio_sid: <%= ENV["TWILIO_SID"] %>
|
49
|
+
twilio_token: <%= ENV["TWILIO_TOKEN"] %>
|
50
|
+
twilio_phone_number: <%= ENV["TWILIO_PHONE_NUMBER"] %>
|
51
|
+
|
30
52
|
== Configuring views
|
31
53
|
|
32
54
|
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:
|
@@ -8,51 +8,21 @@ class Devise::PhoneVerificationsController < DeviseController
|
|
8
8
|
|
9
9
|
# POST /resource/phone_verification
|
10
10
|
def create
|
11
|
-
|
12
|
-
# self.set_default_phone_attributes_and_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
11
|
end
|
23
12
|
|
24
13
|
# GET /resource/phone_verification/insert
|
25
14
|
def insert
|
26
|
-
# puts "current_user"
|
27
|
-
# puts current_user
|
28
|
-
# puts "current_user methods"
|
29
|
-
# puts current_user.methods
|
30
|
-
# puts "User.all.first"
|
31
|
-
# puts User.all.first
|
32
|
-
# puts "the same?"
|
33
|
-
# puts User.all.first == current_user
|
34
|
-
|
35
|
-
# User.all.first.generate_verification_code_and_send_sms
|
36
|
-
# puts current_user.methods
|
37
15
|
current_user.generate_verification_code_and_send_sms
|
38
|
-
|
16
|
+
puts "hey"
|
17
|
+
render nothing: true
|
39
18
|
end
|
40
19
|
|
41
20
|
# GET or POST /resource/phone_verification/consume?sms_token=abcdef
|
42
21
|
def consume
|
43
|
-
|
44
|
-
# puts current_user.phone_number
|
45
22
|
current_user.verify_phone_number_with_code_entered(params[:code_entered])
|
23
|
+
puts "hey2"
|
46
24
|
|
47
|
-
|
48
|
-
|
49
|
-
# if resource.errors.empty?
|
50
|
-
# set_flash_message :notice, :confirmed
|
51
|
-
# sign_in_and_redirect(resource_name, resource)
|
52
|
-
# else
|
53
|
-
# render :new
|
54
|
-
# end
|
55
|
-
|
25
|
+
render nothing: true
|
56
26
|
end
|
57
27
|
|
58
28
|
protected
|
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,54 +10,6 @@ require 'devise_phone/controllers/helpers'
|
|
10
10
|
require 'devise_phone/rails'
|
11
11
|
|
12
12
|
module Devise
|
13
|
-
|
14
|
-
# Get the phone class from the phone reference object.
|
15
|
-
def self.phone
|
16
|
-
@@phone_ref.get
|
17
|
-
end
|
18
|
-
|
19
|
-
# Set the phone reference object to access the phone.
|
20
|
-
def self.phone=(class_name)
|
21
|
-
@@phone_ref = ref(class_name)
|
22
|
-
end
|
23
|
-
self.phone = "Devise::Phone"
|
24
|
-
# mattr_accessor :sms_confirm_within
|
25
|
-
# @@sms_confirm_within = 2.days
|
26
|
-
# mattr_accessor :sms_confirmation_keys
|
27
|
-
# @@sms_confirmation_keys = [:email]
|
28
|
-
# def self.setup
|
29
|
-
# yield self
|
30
|
-
# end
|
31
|
-
|
32
|
-
# class Getter
|
33
|
-
# def initialize name
|
34
|
-
# @name = name
|
35
|
-
# end
|
36
|
-
|
37
|
-
# def get
|
38
|
-
# ActiveSupport::Dependencies.constantize(@name)
|
39
|
-
# end
|
40
|
-
# end
|
41
|
-
|
42
|
-
# def self.ref(arg)
|
43
|
-
# if defined?(ActiveSupport::Dependencies::ClassCache)
|
44
|
-
# ActiveSupport::Dependencies::reference(arg)
|
45
|
-
# Getter.new(arg)
|
46
|
-
# else
|
47
|
-
# ActiveSupport::Dependencies.ref(arg)
|
48
|
-
# end
|
49
|
-
# end
|
50
|
-
# # Get the sms sender class from the phone reference object.
|
51
|
-
# def self.sms_sender
|
52
|
-
# @@sms_sender_ref.get
|
53
|
-
# end
|
54
|
-
|
55
|
-
# # Set the smser reference object to access the smser.
|
56
|
-
# def self.sms_sender=(class_name)
|
57
|
-
# @@phone_ref = ref(class_name)
|
58
|
-
# end
|
59
|
-
|
60
|
-
# @@sms_sender = "Devise::SmsSender"
|
61
13
|
end
|
62
14
|
|
63
15
|
Devise.add_module :phone, :model => "models/phone", :controller => :phone_verifications, :route => :phone_verification
|
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
|
-
|
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
|
22
|
+
end
|
49
23
|
|
50
|
-
# def copy_default_smser
|
51
|
-
# copy_file "lib/sms_sender.rb", "lib/devise_sms_sender.rb"
|
52
|
-
# end
|
53
24
|
end
|
54
25
|
end
|
55
26
|
end
|
data/lib/models/phone.rb
CHANGED
@@ -31,76 +31,62 @@ module Devise
|
|
31
31
|
|
32
32
|
included do
|
33
33
|
before_create :set_unverified_phone_attributes, :if => :phone_verification_needed?
|
34
|
-
after_create :
|
34
|
+
after_create :private_generate_verification_code_and_send_sms, :if => :phone_verification_needed?
|
35
|
+
# before_save :remember_old_phone_number
|
36
|
+
before_save :private_generate_verification_code_and_send_sms, :if => :phone_number_changed?
|
35
37
|
end
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
-
# # Confirm a user by setting it's sms_confirmed_at to actual time. If the user
|
40
|
-
# # is already confirmed, add en error to email field
|
41
|
-
# def confirm_sms!
|
42
|
-
# unless_sms_confirmed do
|
43
|
-
# self.sms_confirmation_token = nil
|
44
|
-
# self.sms_confirmed_at = Time.now
|
45
|
-
# save(:validate => false)
|
46
|
-
# end
|
47
|
-
# end
|
48
|
-
|
49
|
-
# # Verifies whether a user is sms-confirmed or not
|
50
|
-
# def confirmed_sms?
|
51
|
-
# !!sms_confirmed_at
|
52
|
-
# end
|
53
|
-
|
54
|
-
# Send confirmation token by sms
|
55
39
|
def generate_verification_code_and_send_sms
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
send_sms_verification_code
|
61
|
-
else
|
62
|
-
self.errors.add(:phone_verification_code, :no_phone_associated)
|
63
|
-
false
|
40
|
+
if(phone_verification_needed?)
|
41
|
+
private_generate_verification_code_and_send_sms
|
42
|
+
end
|
43
|
+
self.save!
|
64
44
|
end
|
65
|
-
end
|
66
45
|
|
67
46
|
def verify_phone_number_with_code_entered(code_entered)
|
68
|
-
if (code_entered == self.phone_verification_code)
|
69
|
-
|
70
|
-
true
|
71
|
-
else
|
72
|
-
self.errors.add(:phone_verification_code, :wrong_code_entered)
|
73
|
-
false
|
47
|
+
if phone_verification_needed? && (code_entered == self.phone_verification_code)
|
48
|
+
mark_phone_as_verified!
|
74
49
|
end
|
75
50
|
end
|
76
51
|
|
52
|
+
private
|
77
53
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
# !sms_confirmation_required? || confirmed_sms? || confirmation_sms_period_valid?
|
90
|
-
# end
|
54
|
+
# def remember_old_phone_number
|
55
|
+
# puts "phone number changed?: "
|
56
|
+
# puts phone_number_changed?
|
57
|
+
# if phone_number.present?
|
58
|
+
# puts "Old phone number before save:"
|
59
|
+
# puts phone_number
|
60
|
+
# @old_phone_number = phone_number
|
61
|
+
# else
|
62
|
+
# @old_phone_number = nil
|
63
|
+
# end
|
64
|
+
# end
|
91
65
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
66
|
+
# def phone_number_changed?
|
67
|
+
# puts "Old phone number after save:"
|
68
|
+
# puts @old_phone_number
|
69
|
+
# if @old_phone_number.present? && phone_number.present?
|
70
|
+
# puts "condition 1"
|
71
|
+
# @old_phone_number != phone_number
|
72
|
+
# elsif @old_phone_number.blank? && phone_number.present?
|
73
|
+
# puts "condition 2"
|
74
|
+
# true
|
75
|
+
# else
|
76
|
+
# puts "condition 3"
|
77
|
+
# set_unverified_phone_attributes
|
78
|
+
# false
|
79
|
+
# end
|
80
|
+
# end
|
96
81
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
82
|
+
def private_generate_verification_code_and_send_sms
|
83
|
+
self.phone_verification_code = generate_phone_verification_code
|
84
|
+
set_unverified_phone_attributes
|
85
|
+
if phone_number.present?
|
86
|
+
send_sms_verification_code
|
87
|
+
end
|
88
|
+
end
|
102
89
|
|
103
|
-
private
|
104
90
|
|
105
91
|
def mark_phone_as_verified!
|
106
92
|
update!(phone_number_verified: true,
|
@@ -109,36 +95,44 @@ module Devise
|
|
109
95
|
phone_verified_at: DateTime.now)
|
110
96
|
end
|
111
97
|
|
112
|
-
#
|
98
|
+
# check if phone verification is needed and set errors here
|
113
99
|
def phone_verification_needed?
|
114
|
-
phone_number.
|
100
|
+
if phone_number.blank?
|
101
|
+
self.errors.add(:phone_verification_code, :empty_phone_number_field)
|
102
|
+
false
|
103
|
+
elsif phone_number_verified
|
104
|
+
self.errors.add(:phone_verification_code, :phone_verification_not_needed)
|
105
|
+
false
|
106
|
+
else
|
107
|
+
true
|
108
|
+
end
|
115
109
|
end
|
116
110
|
|
117
|
-
#
|
118
|
-
# this token is being generated
|
111
|
+
# set attributes to user indicating the phone number is unverified
|
119
112
|
def set_unverified_phone_attributes
|
120
|
-
|
121
113
|
self.phone_number_verified = false
|
122
114
|
self.phone_verification_code_sent_at = DateTime.now
|
123
115
|
self.phone_verified_at = nil
|
124
116
|
# removes all white spaces, hyphens, and parenthesis
|
125
|
-
self.phone_number
|
117
|
+
if self.phone_number
|
118
|
+
self.phone_number.gsub!(/[\s\-\(\)]+/, '')
|
119
|
+
end
|
126
120
|
end
|
127
121
|
|
122
|
+
# return 6 digits random code a-z,0-9
|
128
123
|
def generate_phone_verification_code
|
129
124
|
verification_code = SecureRandom.hex(3)
|
130
125
|
verification_code
|
131
126
|
end
|
132
127
|
|
128
|
+
# sends a message to number indicated in the secrets.yml
|
133
129
|
def send_sms_verification_code
|
134
|
-
puts "in the private method"
|
135
|
-
|
136
130
|
number_to_send_to = self.phone_number
|
137
131
|
verification_code = self.phone_verification_code
|
138
132
|
|
139
|
-
twilio_sid =
|
140
|
-
twilio_token =
|
141
|
-
twilio_phone_number =
|
133
|
+
twilio_sid = Rails.application.secrets.twilio_sid
|
134
|
+
twilio_token = Rails.application.secrets.twilio_token
|
135
|
+
twilio_phone_number = Rails.application.secrets.twilio_phone_number
|
142
136
|
|
143
137
|
@twilio_client = Twilio::REST::Client.new twilio_sid, twilio_token
|
144
138
|
|
@@ -148,56 +142,25 @@ module Devise
|
|
148
142
|
:body => "Hi! This is MathCrunch. Your verification code is #{verification_code}"
|
149
143
|
)
|
150
144
|
end
|
145
|
+
#end of private methods
|
151
146
|
|
152
|
-
module ClassMethods
|
147
|
+
# module ClassMethods # 'public' methods for class user
|
153
148
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
else
|
161
|
-
self.errors.add(:phone_verification_code, :no_phone_associated)
|
162
|
-
false
|
163
|
-
end
|
164
|
-
end
|
149
|
+
# def generate_verification_code_and_send_sms
|
150
|
+
# if(phone_verification_needed?)
|
151
|
+
# private_generate_verification_code_and_send_sms
|
152
|
+
# end
|
153
|
+
# self.save!
|
154
|
+
# end
|
165
155
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
self.errors.add(:phone_verification_code, :wrong_code_entered)
|
172
|
-
false
|
173
|
-
end
|
174
|
-
end
|
156
|
+
# def verify_phone_number_with_code_entered(code_entered)
|
157
|
+
# if phone_verification_needed? && (code_entered == self.phone_verification_code)
|
158
|
+
# mark_phone_as_verified!
|
159
|
+
# end
|
160
|
+
# end
|
175
161
|
|
176
|
-
end
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
# def mark_phone_as_verified!
|
181
|
-
# update!(phone_number_verified: true,
|
182
|
-
# phone_verification_code: nil,
|
183
|
-
# phone_verification_code_sent_at: nil,
|
184
|
-
# phone_verified_at: DateTime.now)
|
185
|
-
# end
|
162
|
+
# end #end of ClassMethods
|
186
163
|
|
187
|
-
# def verify_phone_number_with_code_entered(code_entered)
|
188
|
-
# if self.phone_verification_code == code_entered
|
189
|
-
# mark_phone_as_verified!
|
190
|
-
# end
|
191
|
-
# end
|
192
|
-
|
193
|
-
# def set_unverified_phone_attributes_and_send_verification_code
|
194
|
-
# self.set_verified_phone_attributes
|
195
|
-
# if self.save!
|
196
|
-
# send_sms_for_verification_code
|
197
|
-
# end
|
198
|
-
# end
|
199
|
-
|
200
|
-
# end
|
201
164
|
end
|
202
165
|
end
|
203
166
|
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.1662
|
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-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|