devise_phone 0.0.17 → 0.0.161
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +0 -3
- data/README.rdoc +31 -55
- data/app/controllers/devise/phone_verifications_controller.rb +34 -27
- data/app/views/devise/phone/insert.html.erb +11 -0
- data/app/views/devise/phone/new.html.erb +12 -0
- data/config/locales/en.yml +11 -11
- data/lib/devise_phone.rb +16 -0
- data/lib/devise_phone/controllers/helpers.rb +9 -0
- data/lib/devise_phone/routes.rb +2 -2
- data/lib/devise_phone/version.rb +1 -1
- data/lib/generators/devise_phone/install_generator.rb +31 -2
- data/lib/generators/devise_phone/views_generator.rb +1 -12
- data/lib/generators/templates/lib/sms_sender.rb +19 -0
- data/lib/models/phone.rb +177 -76
- metadata +6 -5
- data/app/views/devise/phone/_activate_phone.html.erb +0 -7
- data/app/views/devise/phone/_resend_code.html.erb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ba4d973bd2631e836e0371a6a06c87706ae608f
|
4
|
+
data.tar.gz: 57309688fe242c0df7b59b862dc65e4b1fb3cb6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb9ae231ee52611e6bdfa2c87f269b759eb767c5a0c66050ae30221bb23589813654f73ca81fe0a09dfad4089598adf1970f71af230d7c3e64ad69f8863afc3a
|
7
|
+
data.tar.gz: 3efc7963c038294b4860b831b3dbcba0a6839c8f19284f6888eaac414200a6c333c87725800995af293337e0afaf280eef59b56bdff93b9906baf90a8ada31d9
|
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -1,54 +1,40 @@
|
|
1
|
-
=
|
1
|
+
= devise_sms_confirmation
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
Devise Phone is a snap-in for Devise that will make any resource activable via SMS.
|
4
|
+
The user will receive an SMS with a token that can be entered on the site to activate the account.
|
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
|
5
12
|
|
6
13
|
Add DevisePhone to your Gemfile (and Devise and TwilioRuby if you weren't using them):
|
7
14
|
|
8
|
-
gem 'devise'
|
9
|
-
gem 'twilio-ruby'
|
10
|
-
gem 'devise_phone'
|
15
|
+
gem 'devise', '~> 3.4.1'
|
16
|
+
gem 'twilio-ruby', '~> 4.2.0'
|
17
|
+
gem 'devise_phone', '~> 0.0.1'
|
11
18
|
|
12
19
|
=== Automatic installation
|
13
20
|
|
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
|
-
|
20
21
|
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:
|
21
22
|
|
22
23
|
rails generate devise_phone:install
|
23
24
|
|
24
|
-
When you are done, you are ready to add DevisePhone to any of your Devise models using the following generator:
|
25
|
+
When you are done, you are ready to add DevisePhone to any of your Devise models using the following generator:
|
25
26
|
|
26
27
|
rails generate devise_phone MODEL
|
27
28
|
|
28
29
|
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
30
|
|
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
|
-
|
45
31
|
== Configuring views
|
46
32
|
|
47
33
|
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:
|
48
34
|
|
49
35
|
rails generate devise_phone:views
|
50
36
|
|
51
|
-
You can also use the generator to generate scoped views:
|
37
|
+
You can also use the generator to generate scoped views:
|
52
38
|
|
53
39
|
rails generate devise_phone:views users
|
54
40
|
|
@@ -56,40 +42,30 @@ Please refer to {Devise's README}[http://github.com/plataformatec/devise] for mo
|
|
56
42
|
|
57
43
|
== Usage
|
58
44
|
|
59
|
-
|
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
|
45
|
+
The model is specular to the Devise's own Confirmable model. It only requires the user to supply a valid phone number.
|
67
46
|
|
68
|
-
|
47
|
+
On registration it will send an SMS with a token to be inserted to complete activation process.
|
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.
|
69
51
|
|
70
|
-
|
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
|
52
|
+
== Controller filter
|
82
53
|
|
83
|
-
|
84
|
-
<%= render 'devise/phone/resend_code' %>
|
54
|
+
DevisePhone extends your controllers with a <tt>require_sms_activated!</tt> method. Use it to restrict part of the site to "confirmed users" only
|
85
55
|
|
86
|
-
|
87
|
-
<%= render 'devise/phone/activate_phone' %>
|
56
|
+
== I18n
|
88
57
|
|
58
|
+
DevisePhone installs a localizable file in your config/locales folder.
|
89
59
|
|
90
60
|
== Contributing to devise_phone
|
91
61
|
|
92
|
-
*
|
62
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
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.
|
93
69
|
|
94
70
|
== Copyright
|
95
71
|
|
@@ -1,38 +1,45 @@
|
|
1
1
|
class Devise::PhoneVerificationsController < DeviseController
|
2
2
|
|
3
3
|
# GET /resource/phone_verification/new
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
def new
|
5
|
+
build_resource({})
|
6
|
+
render :new
|
7
|
+
end
|
8
8
|
|
9
9
|
# POST /resource/phone_verification
|
10
|
-
|
11
|
-
|
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
|
12
23
|
|
13
|
-
# GET /resource/phone_verification/
|
14
|
-
def
|
15
|
-
|
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
|
24
|
+
# GET /resource/phone_verification/insert
|
25
|
+
def insert
|
26
|
+
build_resource({})
|
21
27
|
end
|
22
28
|
|
23
|
-
# GET or POST /resource/phone_verification/
|
24
|
-
def
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
29
|
+
# GET or POST /resource/phone_verification/consume?sms_token=abcdef
|
30
|
+
def consume
|
31
|
+
|
32
|
+
self.verify_phone_number_with_code_entered(params[:code_entered])
|
33
|
+
|
34
|
+
# self.resource = resource_class.verify_phone_number_with_code_entered(params[:code_entered])
|
35
|
+
|
36
|
+
# if resource.errors.empty?
|
37
|
+
# set_flash_message :notice, :confirmed
|
38
|
+
# sign_in_and_redirect(resource_name, resource)
|
39
|
+
# else
|
40
|
+
# render :new
|
41
|
+
# end
|
42
|
+
|
36
43
|
end
|
37
44
|
|
38
45
|
protected
|
@@ -0,0 +1,11 @@
|
|
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" %>
|
@@ -0,0 +1,12 @@
|
|
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" %>
|
data/config/locales/en.yml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
en:
|
2
2
|
errors:
|
3
3
|
messages:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
4
|
+
no_phone_associated: "No phone associated"
|
5
|
+
sms_already_confirmed: "This token has been already used"
|
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,6 +10,22 @@ 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"
|
13
29
|
end
|
14
30
|
|
15
31
|
Devise.add_module :phone, :model => "models/phone", :controller => :phone_verifications, :route => :phone_verification
|
@@ -1,3 +1,12 @@
|
|
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
|
2
11
|
end
|
3
12
|
ActionController::Base.send :include, DevisePhone::Controllers::Helpers
|
data/lib/devise_phone/routes.rb
CHANGED
@@ -4,8 +4,8 @@ module ActionDispatch::Routing
|
|
4
4
|
protected
|
5
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
|
+
match :consume, :path => mapping.path_names[:consume], :as => :consume
|
8
|
+
get :insert, :path => mapping.path_names[:insert], :as => :insert
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
data/lib/devise_phone/version.rb
CHANGED
@@ -16,11 +16,40 @@ module DevisePhone
|
|
16
16
|
if old_content.match(Regexp.new(/^\s# ==> Configuration for :phone\n/))
|
17
17
|
false
|
18
18
|
end
|
19
|
-
|
20
19
|
end
|
21
|
-
|
22
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
|
+
|
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
|
23
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
|
+
end
|
24
53
|
end
|
25
54
|
end
|
26
55
|
end
|
@@ -3,19 +3,8 @@ 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
|
6
|
+
source_root File.expand_path("../../../../app/views", __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
|
-
|
19
8
|
end
|
20
9
|
end
|
21
10
|
end
|
@@ -0,0 +1,19 @@
|
|
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
|
data/lib/models/phone.rb
CHANGED
@@ -2,113 +2,214 @@ 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
|
+
#
|
5
29
|
module Phone
|
6
30
|
extend ActiveSupport::Concern
|
7
31
|
|
8
32
|
included do
|
9
|
-
before_create :
|
10
|
-
|
11
|
-
# before_save :remember_old_phone_number
|
12
|
-
after_save :private_generate_verification_code_and_send_sms, :if => :regenerate_phone_verification_needed?
|
33
|
+
before_create :set_phone_attributes, :if => :phone_verification_needed?
|
34
|
+
after_create :generate_verification_code_and_send_sms, :if => :phone_verification_needed?
|
13
35
|
end
|
14
36
|
|
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
|
15
53
|
def generate_verification_code_and_send_sms
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
54
|
+
if(self.phone_number?)
|
55
|
+
self.phone_verification_code = generate_phone_verification_code
|
56
|
+
::Devise.sms_sender.send_sms_verification_code_to(self)
|
26
57
|
else
|
58
|
+
# self.errors.add(:sms_confirmation_token, :no_phone_associated)
|
27
59
|
false
|
28
60
|
end
|
29
61
|
end
|
30
62
|
|
31
|
-
|
63
|
+
# # Resend sms confirmation token. This method does not need to generate a new token.
|
64
|
+
# def resend_sms_token
|
65
|
+
# unless_sms_confirmed { send_sms_token }
|
66
|
+
# end
|
32
67
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
send_sms_verification_code
|
38
|
-
end
|
39
|
-
end
|
68
|
+
# Overwrites active? from Devise::Models::Activatable for sms confirmation
|
69
|
+
# by verifying whether a user is active to sign in or not. If the user
|
70
|
+
# is already confirmed, it should never be blocked. Otherwise we need to
|
71
|
+
# calculate if the confirm time has not expired for this user.
|
40
72
|
|
73
|
+
# def active?
|
74
|
+
# !sms_confirmation_required? || confirmed_sms? || confirmation_sms_period_valid?
|
75
|
+
# end
|
41
76
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
phone_verified_at: DateTime.now)
|
47
|
-
end
|
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
|
48
81
|
|
49
|
-
|
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
|
+
|
88
|
+
protected
|
89
|
+
|
90
|
+
# Callback to overwrite if an sms confirmation is required or not.
|
50
91
|
def phone_verification_needed?
|
51
|
-
|
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
|
92
|
+
phone_number.present? && !phone_number_verified
|
60
93
|
end
|
61
94
|
|
62
|
-
def
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
+
# Generates a new random token for confirmation, and stores the time
|
134
|
+
# this token is being generated
|
135
|
+
def set_phone_attributes
|
75
136
|
|
76
|
-
# set attributes to user indicating the phone number is unverified
|
77
|
-
def set_unverified_phone_attributes
|
78
137
|
self.phone_number_verified = false
|
79
138
|
self.phone_verification_code_sent_at = DateTime.now
|
80
139
|
self.phone_verified_at = nil
|
81
140
|
# removes all white spaces, hyphens, and parenthesis
|
82
|
-
|
83
|
-
self.phone_number.gsub!(/[\s\-\(\)]+/, '')
|
84
|
-
end
|
141
|
+
self.phone_number.gsub!(/[\s\-\(\)]+/, '')
|
85
142
|
end
|
86
143
|
|
87
|
-
# return 4 digits random code 0-9
|
88
144
|
def generate_phone_verification_code
|
89
|
-
|
145
|
+
# begin
|
146
|
+
verification_code = SecureRandom.hex(3)
|
147
|
+
# end while self.class.exists?(phone_verification_code: verification_code)
|
90
148
|
verification_code
|
91
149
|
end
|
92
150
|
|
93
|
-
#
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
151
|
+
# def generate_sms_token!
|
152
|
+
# generate_sms_token && save(:validate => false)
|
153
|
+
# end
|
154
|
+
|
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
|
187
|
+
end
|
111
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)
|
212
|
+
end
|
112
213
|
end
|
113
214
|
end
|
114
215
|
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.161
|
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-18 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/insert.html.erb
|
100
|
+
- app/views/devise/phone/new.html.erb
|
101
101
|
- config/locales/en.yml
|
102
102
|
- devise_phone.gemspec
|
103
103
|
- lib/devise_phone.rb
|
@@ -114,6 +114,7 @@ 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
|
117
118
|
- lib/models/phone.rb
|
118
119
|
- rails/init.rb
|
119
120
|
homepage: https://github.com/tjhubert/devise_phone
|
@@ -136,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
137
|
version: '0'
|
137
138
|
requirements: []
|
138
139
|
rubyforge_project:
|
139
|
-
rubygems_version: 2.4.
|
140
|
+
rubygems_version: 2.4.8
|
140
141
|
signing_key:
|
141
142
|
specification_version: 4
|
142
143
|
summary: Send SMS to verify phone number
|
@@ -1,7 +0,0 @@
|
|
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 %>
|