devise_phone 0.0.164 → 0.0.165

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2908cbcc2f76d5f90a6a319864e65b980a0b3754
4
- data.tar.gz: e1e4247c124c076258e20ceef4c9b3741e1b4aff
3
+ metadata.gz: 70a83b26e9d151e9b9a8b6fc131fc0f0191a58dd
4
+ data.tar.gz: 009202764b0c4c904cc1f71484486bf3a3a9a73a
5
5
  SHA512:
6
- metadata.gz: 06164a61896a8d952d9b12cac52f5536a5716f7c08621782ab32091b08256ed81228c963f01e8c921dd41a6d676cfa239f0921a4ea22a221353e056cc413fb9a
7
- data.tar.gz: 0e0ecf25d74cf692afb78dcc813bc410e282a0b2870578a62509e3aa47236cc11bb548a2f7f2c93e36a583b1aaa0946d62aa968bf7b6b73b9669780126731ce6
6
+ metadata.gz: f819275b7113340d0f043da247f723132d2ef5a94d4578b57d0630c013fa703603ba2f66d36885c05ac75d422e417305d16fe2f6118bb042d2271c520ce3f2d8
7
+ data.tar.gz: 74bc57e15053599775a315c518edba3ee87bd9bcdc7371752e6a5f7612b79107835da62524597adce019c5b93763b7546dc0d9e924f9d1f84bf6b41b545e12c0
@@ -9,7 +9,7 @@ class Devise::PhoneVerificationsController < DeviseController
9
9
  # POST /resource/phone_verification
10
10
  def create
11
11
 
12
- self.send_verification_code
12
+ # self.set_default_phone_attributes_and_send_verification_code
13
13
 
14
14
  # self.resource = resource_class.send_verification_code
15
15
 
@@ -23,13 +23,26 @@ class Devise::PhoneVerificationsController < DeviseController
23
23
 
24
24
  # GET /resource/phone_verification/insert
25
25
  def insert
26
- build_resource({})
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
+ current_user.generate_verification_code_and_send_sms
38
+ # build_resource({})
27
39
  end
28
40
 
29
41
  # GET or POST /resource/phone_verification/consume?sms_token=abcdef
30
42
  def consume
31
43
 
32
- self.verify_phone_number_with_code_entered(params[:code_entered])
44
+ # puts current_user.phone_number
45
+ current_user.verify_phone_number_with_code_entered(params[:code_entered])
33
46
 
34
47
  # self.resource = resource_class.verify_phone_number_with_code_entered(params[:code_entered])
35
48
 
@@ -0,0 +1,9 @@
1
+ <h2>Activate Phone Number</h2>
2
+
3
+ <%= form_for(resource, :as => resource_name, :url => consume_user_phone_verification_path(resource_name), :html => { :method => :post }, remote: true) 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 %>
@@ -0,0 +1,6 @@
1
+ <%= form_for(resource, :as => resource_name, :url => insert_user_phone_verification_path(resource_name), :html => { :method => :get }, remote: true) do |f| %>
2
+ <%= devise_error_messages! %>
3
+
4
+ <%= f.submit "Resend Phone Verification Code" %>
5
+
6
+ <% end %>
data/lib/devise_phone.rb CHANGED
@@ -10,22 +10,54 @@ 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"
13
24
  # mattr_accessor :sms_confirm_within
14
25
  # @@sms_confirm_within = 2.days
15
26
  # mattr_accessor :sms_confirmation_keys
16
27
  # @@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
28
+ # def self.setup
29
+ # yield self
30
+ # end
22
31
 
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
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
27
59
 
28
- self.sms_sender = "Devise::SmsSender"
60
+ # @@sms_sender = "Devise::SmsSender"
29
61
  end
30
62
 
31
63
  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
@@ -1,3 +1,3 @@
1
1
  module DevisePhone
2
- VERSION = "0.0.164".freeze
2
+ VERSION = "0.0.165".freeze
3
3
  end
@@ -47,9 +47,9 @@ module DevisePhone
47
47
  # copy_file "../../../config/locales/en.yml", "config/locales/devise_phone.en.yml"
48
48
  # end
49
49
 
50
- def copy_default_smser
51
- copy_file "lib/sms_sender.rb", "lib/devise_sms_sender.rb"
52
- end
50
+ # def copy_default_smser
51
+ # copy_file "lib/sms_sender.rb", "lib/devise_sms_sender.rb"
52
+ # end
53
53
  end
54
54
  end
55
55
  end
@@ -2,7 +2,7 @@ require 'generators/devise/views_generator'
2
2
 
3
3
  module DevisePhone
4
4
  module Generators
5
- class ViewsGenerator < Devise::Generators::ViewsGenerator
5
+ class ViewsGenerator
6
6
  source_root File.expand_path("../../../../app/views/devise", __FILE__)
7
7
  desc 'Copies all DevisePhone views to your application.'
8
8
 
data/lib/models/phone.rb CHANGED
@@ -30,10 +30,12 @@ module Devise
30
30
  extend ActiveSupport::Concern
31
31
 
32
32
  included do
33
- before_create :set_phone_attributes, :if => :phone_verification_needed?
33
+ before_create :set_unverified_phone_attributes, :if => :phone_verification_needed?
34
34
  after_create :generate_verification_code_and_send_sms, :if => :phone_verification_needed?
35
35
  end
36
36
 
37
+
38
+
37
39
  # # Confirm a user by setting it's sms_confirmed_at to actual time. If the user
38
40
  # # is already confirmed, add en error to email field
39
41
  # def confirm_sms!
@@ -51,15 +53,28 @@ module Devise
51
53
 
52
54
  # Send confirmation token by sms
53
55
  def generate_verification_code_and_send_sms
56
+ puts "in the public method"
54
57
  if(self.phone_number?)
55
- self.phone_verification_code = generate_phone_verification_code
56
- ::Devise.sms_sender.send_sms_verification_code_to(self)
58
+ puts "generating code"
59
+ update!(phone_verification_code: generate_phone_verification_code)
60
+ send_sms_verification_code
61
+ else
62
+ self.errors.add(:phone_verification_code, :no_phone_associated)
63
+ false
64
+ end
65
+ end
66
+
67
+ def verify_phone_number_with_code_entered(code_entered)
68
+ if (code_entered == self.phone_verification_code)
69
+ mark_phone_as_verified!
70
+ true
57
71
  else
58
- # self.errors.add(:sms_confirmation_token, :no_phone_associated)
72
+ self.errors.add(:phone_verification_code, :wrong_code_entered)
59
73
  false
60
74
  end
61
75
  end
62
76
 
77
+
63
78
  # # Resend sms confirmation token. This method does not need to generate a new token.
64
79
  # def resend_sms_token
65
80
  # unless_sms_confirmed { send_sms_token }
@@ -85,54 +100,23 @@ module Devise
85
100
  # self.sms_confirmed_at = Time.now
86
101
  # end
87
102
 
88
- protected
103
+ private
104
+
105
+ def mark_phone_as_verified!
106
+ update!(phone_number_verified: true,
107
+ phone_verification_code: nil,
108
+ phone_verification_code_sent_at: nil,
109
+ phone_verified_at: DateTime.now)
110
+ end
89
111
 
90
112
  # Callback to overwrite if an sms confirmation is required or not.
91
113
  def phone_verification_needed?
92
114
  phone_number.present? && !phone_number_verified
93
115
  end
94
116
 
95
- # def sms_confirmation_required?
96
- # !confirmed_sms?
97
- # end
98
-
99
- # Checks if the confirmation for the user is within the limit time.
100
- # We do this by calculating if the difference between today and the
101
- # confirmation sent date does not exceed the confirm in time configured.
102
- # Confirm_in is a model configuration, must always be an integer value.
103
- #
104
- # Example:
105
- #
106
- # # sms_confirm_within = 1.day and sms_confirmation_sent_at = today
107
- # confirmation_period_valid? # returns true
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
117
  # Generates a new random token for confirmation, and stores the time
134
118
  # this token is being generated
135
- def set_phone_attributes
119
+ def set_unverified_phone_attributes
136
120
 
137
121
  self.phone_number_verified = false
138
122
  self.phone_verification_code_sent_at = DateTime.now
@@ -142,74 +126,78 @@ module Devise
142
126
  end
143
127
 
144
128
  def generate_phone_verification_code
145
- # begin
146
129
  verification_code = SecureRandom.hex(3)
147
- # end while self.class.exists?(phone_verification_code: verification_code)
148
130
  verification_code
149
131
  end
150
132
 
151
- # def generate_sms_token!
152
- # generate_sms_token && save(:validate => false)
153
- # end
133
+ def send_sms_verification_code
134
+ puts "in the private method"
135
+
136
+ number_to_send_to = self.phone_number
137
+ verification_code = self.phone_verification_code
154
138
 
155
- module ClassMethods
156
- # # Attempt to find a user by it's email. If a record is found, send a new
157
- # # sms token instructions to it. If not user is found, returns a new user
158
- # # with an email not found error.
159
- # # Options must contain the user email
160
- # def send_sms_token(attributes={})
161
- # sms_confirmable = find_or_initialize_with_errors(sms_confirmation_keys, attributes, :not_found)
162
- # sms_confirmable.resend_sms_token if sms_confirmable.persisted?
163
- # sms_confirmable
164
- # end
165
-
166
- # # Find a user by it's sms confirmation token and try to confirm it.
167
- # # If no user is found, returns a new user with an error.
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
139
+ twilio_sid = "ACd35391c08cde7926e2295d1812ada918"
140
+ twilio_token = "44d79a36adb3d54cc15711d94d149119"
141
+ twilio_phone_number = "6502810746"
142
+
143
+ @twilio_client = Twilio::REST::Client.new twilio_sid, twilio_token
144
+
145
+ @twilio_client.account.sms.messages.create(
146
+ :from => "+1#{twilio_phone_number}",
147
+ :to => number_to_send_to,
148
+ :body => "Hi! This is MathCrunch. Your verification code is #{verification_code}"
149
+ )
150
+ end
151
+
152
+ module ClassMethods
153
+
154
+ def generate_verification_code_and_send_sms
155
+ puts "in the public method"
156
+ if(self.phone_number?)
157
+ puts "generating code"
158
+ update!(phone_verification_code: generate_phone_verification_code)
159
+ send_sms_verification_code
160
+ else
161
+ self.errors.add(:phone_verification_code, :no_phone_associated)
162
+ false
187
163
  end
164
+ end
188
165
 
189
- def send_verification_code
190
- self.set_phone_attributes
191
- if self.save!
192
- send_sms_for_phone_verification
193
- end
166
+ def verify_phone_number_with_code_entered(code_entered)
167
+ if (code_entered == self.phone_verification_code)
168
+ mark_phone_as_verified!
169
+ true
170
+ else
171
+ self.errors.add(:phone_verification_code, :wrong_code_entered)
172
+ false
194
173
  end
174
+ end
175
+
176
+ end
195
177
 
196
- # # Generates a small token that can be used conveniently on SMS's.
197
- # # The token is 5 chars long and uppercased.
178
+
198
179
 
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
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
205
186
 
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
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
210
192
 
211
- # Devise::Models.config(self, :sms_confirm_within, :sms_confirmation_keys)
212
- end
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
213
201
  end
214
202
  end
215
203
  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.164
4
+ version: 0.0.165
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-18 00:00:00.000000000 Z
11
+ date: 2015-06-19 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/insert.html.erb
100
- - app/views/devise/phone/new.html.erb
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
@@ -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