devise_sms_confirmable 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ad0caa6d14ad6b8406d429dd6b95e6048095238dd92e2f430217748db18f8d5b
4
+ data.tar.gz: 847c743ab1ff45c74d69e7759d22ea705a793a1b407499f3437ca4de4936b602
5
+ SHA512:
6
+ metadata.gz: b28925a7656bdda3d2887051684f711b5e7397ec9bda1d8b536fb40a2b72cbba5388821b9985e7e3bcbc005ca98d71443e80f2d68a93163fbdbb6b93d0839e17
7
+ data.tar.gz: 40fec62988c8c698a690edbfe46409321ff778f559c4b32023840fed3baf7ef2a0ee892ed7900e1352a7b8f6331b94cf2711f31f08f65ac08cacf1595de2725a
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /vendor/bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.17.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at yushiro.ma2ta2.21@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in devise_sms_confirmable.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,166 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ devise_sms_confirmable (0.1.1)
5
+ devise (>= 4.6.2)
6
+ railties (>= 5.1.0)
7
+ textris (~> 0.7)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actioncable (5.2.3)
13
+ actionpack (= 5.2.3)
14
+ nio4r (~> 2.0)
15
+ websocket-driver (>= 0.6.1)
16
+ actionmailer (5.2.3)
17
+ actionpack (= 5.2.3)
18
+ actionview (= 5.2.3)
19
+ activejob (= 5.2.3)
20
+ mail (~> 2.5, >= 2.5.4)
21
+ rails-dom-testing (~> 2.0)
22
+ actionpack (5.2.3)
23
+ actionview (= 5.2.3)
24
+ activesupport (= 5.2.3)
25
+ rack (~> 2.0)
26
+ rack-test (>= 0.6.3)
27
+ rails-dom-testing (~> 2.0)
28
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
29
+ actionview (5.2.3)
30
+ activesupport (= 5.2.3)
31
+ builder (~> 3.1)
32
+ erubi (~> 1.4)
33
+ rails-dom-testing (~> 2.0)
34
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
35
+ activejob (5.2.3)
36
+ activesupport (= 5.2.3)
37
+ globalid (>= 0.3.6)
38
+ activemodel (5.2.3)
39
+ activesupport (= 5.2.3)
40
+ activerecord (5.2.3)
41
+ activemodel (= 5.2.3)
42
+ activesupport (= 5.2.3)
43
+ arel (>= 9.0)
44
+ activestorage (5.2.3)
45
+ actionpack (= 5.2.3)
46
+ activerecord (= 5.2.3)
47
+ marcel (~> 0.3.1)
48
+ activesupport (5.2.3)
49
+ concurrent-ruby (~> 1.0, >= 1.0.2)
50
+ i18n (>= 0.7, < 2)
51
+ minitest (~> 5.1)
52
+ tzinfo (~> 1.1)
53
+ arel (9.0.0)
54
+ bcrypt (3.1.13)
55
+ builder (3.2.3)
56
+ concurrent-ruby (1.1.5)
57
+ crass (1.0.4)
58
+ devise (4.6.2)
59
+ bcrypt (~> 3.0)
60
+ orm_adapter (~> 0.1)
61
+ railties (>= 4.1.0, < 6.0)
62
+ responders
63
+ warden (~> 1.2.3)
64
+ diff-lcs (1.3)
65
+ erubi (1.8.0)
66
+ globalid (0.4.2)
67
+ activesupport (>= 4.2.0)
68
+ i18n (1.6.0)
69
+ concurrent-ruby (~> 1.0)
70
+ loofah (2.2.3)
71
+ crass (~> 1.0.2)
72
+ nokogiri (>= 1.5.9)
73
+ mail (2.7.1)
74
+ mini_mime (>= 0.1.1)
75
+ marcel (0.3.3)
76
+ mimemagic (~> 0.3.2)
77
+ method_source (0.9.2)
78
+ mimemagic (0.3.3)
79
+ mini_mime (1.0.2)
80
+ mini_portile2 (2.4.0)
81
+ minitest (5.11.3)
82
+ nio4r (2.4.0)
83
+ nokogiri (1.10.3)
84
+ mini_portile2 (~> 2.4.0)
85
+ orm_adapter (0.5.0)
86
+ phony (2.18.5)
87
+ rack (2.0.7)
88
+ rack-test (1.1.0)
89
+ rack (>= 1.0, < 3)
90
+ rails (5.2.3)
91
+ actioncable (= 5.2.3)
92
+ actionmailer (= 5.2.3)
93
+ actionpack (= 5.2.3)
94
+ actionview (= 5.2.3)
95
+ activejob (= 5.2.3)
96
+ activemodel (= 5.2.3)
97
+ activerecord (= 5.2.3)
98
+ activestorage (= 5.2.3)
99
+ activesupport (= 5.2.3)
100
+ bundler (>= 1.3.0)
101
+ railties (= 5.2.3)
102
+ sprockets-rails (>= 2.0.0)
103
+ rails-dom-testing (2.0.3)
104
+ activesupport (>= 4.2.0)
105
+ nokogiri (>= 1.6)
106
+ rails-html-sanitizer (1.0.4)
107
+ loofah (~> 2.2, >= 2.2.2)
108
+ railties (5.2.3)
109
+ actionpack (= 5.2.3)
110
+ activesupport (= 5.2.3)
111
+ method_source
112
+ rake (>= 0.8.7)
113
+ thor (>= 0.19.0, < 2.0)
114
+ rake (12.3.0)
115
+ render_anywhere (0.0.12)
116
+ rails (>= 3.0.7)
117
+ responders (3.0.0)
118
+ actionpack (>= 5.0)
119
+ railties (>= 5.0)
120
+ rspec (3.7.0)
121
+ rspec-core (~> 3.7.0)
122
+ rspec-expectations (~> 3.7.0)
123
+ rspec-mocks (~> 3.7.0)
124
+ rspec-core (3.7.1)
125
+ rspec-support (~> 3.7.0)
126
+ rspec-expectations (3.7.0)
127
+ diff-lcs (>= 1.2.0, < 2.0)
128
+ rspec-support (~> 3.7.0)
129
+ rspec-mocks (3.7.0)
130
+ diff-lcs (>= 1.2.0, < 2.0)
131
+ rspec-support (~> 3.7.0)
132
+ rspec-support (3.7.1)
133
+ sprockets (3.7.2)
134
+ concurrent-ruby (~> 1.0)
135
+ rack (> 1, < 3)
136
+ sprockets-rails (3.2.1)
137
+ actionpack (>= 4.0)
138
+ activesupport (>= 4.0)
139
+ sprockets (>= 3.0.0)
140
+ textris (0.7.0)
141
+ actionmailer (>= 4.0)
142
+ activejob (>= 4.2)
143
+ activesupport (>= 4.2)
144
+ phony (~> 2.8)
145
+ render_anywhere (~> 0.0)
146
+ thor (0.20.3)
147
+ thread_safe (0.3.6)
148
+ tzinfo (1.2.5)
149
+ thread_safe (~> 0.1)
150
+ warden (1.2.8)
151
+ rack (>= 2.0.6)
152
+ websocket-driver (0.7.1)
153
+ websocket-extensions (>= 0.1.0)
154
+ websocket-extensions (0.1.4)
155
+
156
+ PLATFORMS
157
+ ruby
158
+
159
+ DEPENDENCIES
160
+ bundler
161
+ devise_sms_confirmable!
162
+ rake
163
+ rspec
164
+
165
+ BUNDLED WITH
166
+ 1.17.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 matsutani
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # DeviseSmsConfirmable
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/devise_sms_confirmable`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'devise_sms_confirmable'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install devise_sms_confirmable
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/devise_sms_confirmable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the DeviseSmsConfirmable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/devise_sms_confirmable/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Devise::SmsConfirmationsController < DeviseController
4
+ # GET /resource/sms_confirmation/new
5
+ def new
6
+ self.resource = resource_class.new
7
+ end
8
+
9
+ # POST /resource/sms_confirmation
10
+ def create
11
+ self.resource = resource_class.send_confirmation_instructions(resource_params)
12
+ yield resource if block_given?
13
+
14
+ if successfully_sent?(resource)
15
+ respond_with({}, location: after_resending_confirmation_instructions_path_for(resource_name))
16
+ else
17
+ respond_with(resource)
18
+ end
19
+ end
20
+
21
+ # GET /resource/sms_confirmation?confirmation_token=abcdef
22
+ def show
23
+ self.resource = resource_class.sms_confirm_by_token(params[:confirmation_token])
24
+ yield resource if block_given?
25
+
26
+ if resource.errors.empty?
27
+ set_flash_message!(:notice, :confirmed)
28
+ respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
29
+ else
30
+ respond_with_navigational(resource.errors, status: :unprocessable_entity){ render :new }
31
+ end
32
+ end
33
+
34
+ protected
35
+
36
+ # The path used after resending confirmation instructions.
37
+ def after_resending_confirmation_instructions_path_for(resource_name)
38
+ is_navigational_format? ? new_session_path(resource_name) : '/'
39
+ end
40
+
41
+ # The path used after confirmation.
42
+ def after_confirmation_path_for(resource_name, resource)
43
+ if signed_in?(resource_name)
44
+ signed_in_root_path(resource)
45
+ else
46
+ new_session_path(resource_name)
47
+ end
48
+ end
49
+
50
+ def translation_scope
51
+ 'devise.sms_confirmations'
52
+ end
53
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ if defined?(Textris)
4
+ class Devise::Texter < Textris::Base
5
+ default from: Devise.sms_sender
6
+
7
+ def confirmation_instructions(record, token, opts={})
8
+ @token = token
9
+ @resource = record
10
+
11
+ text to: record.phone
12
+ end
13
+
14
+ def reset_password_instructions(record, token, opts={})
15
+ @token = token
16
+ @resource = record
17
+
18
+ text to: record.phone
19
+ end
20
+
21
+ def unlock_instructions(record, token, opts={})
22
+ @token = token
23
+ @resource = record
24
+
25
+ text to: record.phone
26
+ end
27
+
28
+ def email_changed(record, token, opts={})
29
+ @token = token
30
+ @resource = record
31
+
32
+ text to: record.phone
33
+ end
34
+
35
+ def password_change(record, token, opts={})
36
+ @token = token
37
+ @resource = record
38
+
39
+ text to: record.phone
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ Welcome <%= @phone %>!
2
+
3
+ You can confirm your account phone number through the link below:
4
+
5
+ <%= sms_confirmation_url(@resource, confirmation_token: @token) %>
@@ -0,0 +1,7 @@
1
+ Hello <%= @phone %>!
2
+
3
+ <% if @resource.try(:unconfirmed_phone?) %>
4
+ We're contacting you to notify you that your phone number is being changed to <%= @resource.unconfirmed_phone %>.
5
+ <% else %>
6
+ We're contacting you to notify you that your phone has been changed to <%= @resource.phone %>.
7
+ <% end %>
@@ -0,0 +1,3 @@
1
+ Hello <%= @resource.phone %>!
2
+
3
+ We're contacting you to notify you that your password has been changed.
@@ -0,0 +1,8 @@
1
+ Hello <%= @resource.phone %>!
2
+
3
+ Someone has requested a link to change your password. You can do this through the link below.
4
+
5
+ <%= edit_password_url(@resource, reset_password_token: @token) %>
6
+
7
+ If you didn't request this, please ignore this phone number.
8
+ Your password won't change until you access the link above and create a new one.
@@ -0,0 +1,7 @@
1
+ Hello <%= @resource.phone %>!
2
+
3
+ Your account has been locked due to an excessive number of unsuccessful sign in attempts.
4
+
5
+ Click the link below to unlock your account:
6
+
7
+ <%= unlock_url(@resource, unlock_token: @token) %>
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "devise_sms_confirmable"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "devise_sms_confirmable/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "devise_sms_confirmable"
8
+ spec.version = DeviseSmsConfirmable::VERSION
9
+ spec.authors = ["matsutani"]
10
+ spec.email = ["yushiro.ma2ta2.21@gmail.com"]
11
+
12
+ spec.summary = %q{Module provide sms confirmation.}
13
+ spec.description = %q{Module provide sms confirmation.}
14
+ spec.homepage = "https://github.com/uuushiro/devise_sms_confirmable"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "devise", ">= 4.6.2"
27
+ spec.add_dependency("railties", ">= 5.1.0")
28
+ spec.add_dependency("textris", "~> 0.7")
29
+
30
+ spec.add_development_dependency "bundler"
31
+ spec.add_development_dependency "rake"
32
+ spec.add_development_dependency "rspec"
33
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeviseSmsConfirmable
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace DeviseSmsConfirmable
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Devise
4
+ module Models
5
+ module SMSAuthenticatable
6
+ extend ActiveSupport::Concern
7
+
8
+ protected
9
+
10
+ def devise_texter
11
+ Devise.texter
12
+ end
13
+
14
+ def send_sms_devise_notification(notification, *args)
15
+ message = devise_texter.send(notification, self, *args)
16
+ message.deliver_now
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,196 @@
1
+ module Devise
2
+ module Models
3
+ module SmsConfirmable
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ before_create :generate_sms_confirmation_token, if: :sms_confirmation_required?
8
+ after_create :skip_reconfirmation_in_callback!, if: :send_sms_confirmation_notification?
9
+ after_commit :send_on_create_sms_confirmation_instructions, on: :create, if: :send_sms_confirmation_notification?
10
+ after_commit :send_sms_reconfirmation_instructions, on: :update, if: :sms_reconfirmation_required?
11
+ before_update :postpone_phone_change_until_confirmation_and_regenerate_confirmation_token, if: :postpone_phone_change?
12
+ end
13
+
14
+ def self.required_fields(klass)
15
+ required_methods = [:sms_confirmed_at, :sms_confirmation_sent_at]
16
+ required_methods << :unconfirmed_phone if klass.reconfirmable
17
+ required_methods
18
+ end
19
+
20
+ def sms_confirm(args={})
21
+ pending_any_sms_confirmation do
22
+ if sms_confirmation_period_expired?
23
+ self.errors.add(:phone, :sms_confirmation_period_expired,
24
+ period: Devise::TimeInflector.time_ago_in_words(self.class.confirm_within.ago))
25
+ return false
26
+ end
27
+
28
+ self.sms_confirmed_at = Time.now.utc
29
+
30
+ saved = if pending_sms_reconfirmation?
31
+ skip_reconfirmation!
32
+ self.phone = unconfirmed_phone
33
+ self.unconfirmed_phone = nil
34
+
35
+ # We need to validate in such cases to enforce e-mail uniqueness
36
+ save(validate: true)
37
+ else
38
+ save(validate: args[:ensure_valid] == true)
39
+ end
40
+
41
+ after_sms_confirmation if saved
42
+ saved
43
+ end
44
+ end
45
+
46
+ # Verifies whether a user is sms-confirmed or not
47
+ def sms_confirmed?
48
+ !!sms_confirmed_at
49
+ end
50
+
51
+ def send_sms_reconfirmation_instructions
52
+ @reconfirmation_required = false
53
+
54
+ unless @skip_confirmation_notification
55
+ send_sms_confirmation_instructions
56
+ end
57
+ end
58
+
59
+ def send_sms_confirmation_instructions
60
+ unless @raw_confirmation_token
61
+ generate_confirmation_token!
62
+ end
63
+
64
+ opts = pending_reconfirmation? ? { to: unconfirmed_phone } : { }
65
+ send_sms_devise_notification(:confirmation_instructions, @raw_confirmation_token, opts)
66
+ end
67
+
68
+ def generate_confirmation_token!
69
+ generate_sms_confirmation_token && save(validate: false)
70
+ end
71
+
72
+ # override Devise::Models::Confirmable#pending_sms_reconfirmation?
73
+ def pending_sms_reconfirmation?
74
+ self.class.reconfirmable && unconfirmed_phone.present?
75
+ end
76
+
77
+ # protected
78
+
79
+ def devise_texter
80
+ Devise.texter
81
+ end
82
+
83
+ def send_sms_devise_notification(notification, *args)
84
+ message = devise_texter.send(notification, self, *args)
85
+ message.deliver_later
86
+ end
87
+
88
+ # A callback initiated after successfully confirming. This can be
89
+ # used to insert your own logic that is only run after the user successfully
90
+ # confirms.
91
+ #
92
+ # Example:
93
+ #
94
+ # def after_confirmation
95
+ # self.update_attribute(:invite_code, nil)
96
+ # end
97
+ #
98
+ def after_sms_confirmation
99
+ end
100
+
101
+ def send_on_create_sms_confirmation_instructions
102
+ send_sms_confirmation_instructions
103
+ end
104
+
105
+ def sms_confirmation_required?
106
+ !sms_confirmed?
107
+ end
108
+
109
+ def sms_confirmation_period_expired?
110
+ self.class.confirm_within && self.sms_confirmation_sent_at && (Time.now.utc > self.sms_confirmation_sent_at.utc + self.class.confirm_within)
111
+ end
112
+
113
+ # Generates a new random token for confirmation, and stores
114
+ # the time this token is being generated in sms_confirmation_sent_at
115
+ def generate_sms_confirmation_token
116
+ if self.confirmation_token && !confirmation_period_expired?
117
+ @raw_confirmation_token = self.confirmation_token
118
+ else
119
+ self.confirmation_token = @raw_confirmation_token = Devise.friendly_token
120
+ self.sms_confirmation_sent_at = Time.now.utc
121
+ end
122
+ end
123
+
124
+ # Checks whether the record requires any confirmation.
125
+ def pending_any_sms_confirmation
126
+ if (!sms_confirmed? || pending_sms_reconfirmation?)
127
+ yield
128
+ else
129
+ self.errors.add(:phone, :already_confirmed)
130
+ false
131
+ end
132
+ end
133
+
134
+ def postpone_phone_change?
135
+ postpone = self.class.reconfirmable &&
136
+ will_save_change_to_phone? &&
137
+ !@bypass_confirmation_postpone &&
138
+ self.phone.present? &&
139
+ (!@skip_reconfirmation_in_callback || !self.phone_in_database.nil?)
140
+ @bypass_confirmation_postpone = false
141
+ postpone
142
+ end
143
+
144
+ def send_sms_confirmation_notification?
145
+ sms_confirmation_required? && !@skip_confirmation_notification && self.phone.present?
146
+ end
147
+
148
+ def sms_reconfirmation_required?
149
+ self.class.reconfirmable && @reconfirmation_required && (self.phone.present? || self.unconfirmed_phone.present?)
150
+ end
151
+
152
+ def postpone_phone_change_until_confirmation_and_regenerate_confirmation_token
153
+ @reconfirmation_required = true
154
+ self.unconfirmed_phone = self.phone
155
+ self.phone = self.phone_in_database
156
+ self.confirmation_token = nil
157
+ generate_sms_confirmation_token
158
+ end
159
+
160
+ module ClassMethods
161
+ def send_sms_confirmation_instructions(attributes={})
162
+ confirmable = find_by_unconfirmed_phone_with_errors(attributes) if reconfirmable
163
+ unless confirmable.try(:persisted?)
164
+ confirmable = find_or_initialize_with_errors(sms_confirmation_keys, attributes, :not_found)
165
+ end
166
+ confirmable.resend_confirmation_instructions if confirmable.persisted?
167
+ confirmable
168
+ end
169
+
170
+ def sms_confirm_by_token(confirmation_token)
171
+ confirmable = find_first_by_auth_conditions(confirmation_token: confirmation_token)
172
+ unless confirmable
173
+ confirmation_digest = Devise.token_generator.digest(self, :confirmation_token, confirmation_token)
174
+ confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_digest)
175
+ end
176
+
177
+ # TODO: replace above lines with
178
+ # confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
179
+ # after enough time has passed that Devise clients do not use digested tokens
180
+
181
+ confirmable.sms_confirm if confirmable.persisted?
182
+ confirmable
183
+ end
184
+ end
185
+
186
+ def find_by_unconfirmed_phone_with_errors(attributes = {})
187
+ attributes = attributes.slice(*sms_confirmation_keys).permit!.to_h if attributes.respond_to? :permit
188
+ unconfirmed_required_attributes = sms_confirmation_keys.map { |k| k == :phone ? :unconfirmed_phone : k }
189
+ unconfirmed_attributes = attributes.symbolize_keys
190
+ unconfirmed_attributes[:unconfirmed_phone] = unconfirmed_attributes.delete(:phone)
191
+ find_or_initialize_with_errors(unconfirmed_required_attributes, unconfirmed_attributes, :not_found)
192
+ end
193
+
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,9 @@
1
+ module ActionDispatch::Routing
2
+ class Mapper
3
+ protected
4
+
5
+ def devise_sms_confirmation(mapping, controllers)
6
+ resource :sms_confirmation, only: [:new, :create, :show], path: mapping.path_names[:sms_confirmation], controller: controllers[:sms_confirmations]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module DeviseSmsConfirmable
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,38 @@
1
+ require 'devise'
2
+ require 'textris'
3
+ require "devise_sms_confirmable/version"
4
+ require "devise_sms_confirmable/rails/routes"
5
+ require 'devise_sms_confirmable/models/sms_confirmable'
6
+ require 'devise_sms_confirmable/engine'
7
+
8
+ module Devise
9
+ mattr_accessor :sms_confirm_within
10
+ @@sms_confirm_within = 2.days
11
+
12
+ mattr_accessor :sms_confirmation_keys
13
+ @@sms_confirmation_keys = [:phone]
14
+
15
+ mattr_accessor :parent_texter
16
+ @@parent_texter = "Textris::Base"
17
+
18
+ # Phone number which sends Devise SMS.
19
+ mattr_accessor :sms_sender
20
+ @@sms_sender = nil
21
+
22
+ # Get the sms sender class from the texter reference object.
23
+ def self.texter
24
+ @@texter_ref.get
25
+ end
26
+
27
+ # Set the smser reference object to access the smser.
28
+ def self.texter=(class_name)
29
+ @@texter_ref = ref(class_name)
30
+ end
31
+
32
+ self.texter = "Devise::Texter"
33
+ end
34
+
35
+ routes = [nil, :new]
36
+ Devise.add_module :sms_confirmable, controller: :sms_confirmations, route: { sms_confirmation: routes }
37
+
38
+ require_relative '../app/texters/devise/texter'
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_sms_confirmable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - matsutani
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: devise
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.6.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.6.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 5.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 5.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: textris
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Module provide sms confirmation.
98
+ email:
99
+ - yushiro.ma2ta2.21@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - app/controllers/devise/sms_confirmations_controller.rb
114
+ - app/texters/devise/texter.rb
115
+ - app/views/devise/texter/confirmation_instructions.text.erb
116
+ - app/views/devise/texter/email_changed.text.erb
117
+ - app/views/devise/texter/password_change.text.erb
118
+ - app/views/devise/texter/reset_password_instructions.text.erb
119
+ - app/views/devise/texter/unlock_instructions.text.erb
120
+ - bin/console
121
+ - bin/setup
122
+ - devise_sms_confirmable.gemspec
123
+ - lib/devise_sms_confirmable.rb
124
+ - lib/devise_sms_confirmable/engine.rb
125
+ - lib/devise_sms_confirmable/models/sms_authenticatable.rb
126
+ - lib/devise_sms_confirmable/models/sms_confirmable.rb
127
+ - lib/devise_sms_confirmable/rails/routes.rb
128
+ - lib/devise_sms_confirmable/version.rb
129
+ homepage: https://github.com/uuushiro/devise_sms_confirmable
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.7.6
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: Module provide sms confirmation.
153
+ test_files: []