devise_token_auth 0.2.0 → 1.0.0.rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of devise_token_auth might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +1 -2
- data/Rakefile +9 -4
- data/app/controllers/devise_token_auth/application_controller.rb +5 -7
- data/app/controllers/devise_token_auth/concerns/resource_finder.rb +2 -2
- data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +18 -10
- data/app/controllers/devise_token_auth/confirmations_controller.rb +2 -2
- data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +17 -23
- data/app/controllers/devise_token_auth/passwords_controller.rb +23 -27
- data/app/controllers/devise_token_auth/registrations_controller.rb +17 -15
- data/app/controllers/devise_token_auth/sessions_controller.rb +8 -12
- data/app/controllers/devise_token_auth/token_validations_controller.rb +3 -3
- data/app/controllers/devise_token_auth/unlocks_controller.rb +10 -12
- data/app/models/devise_token_auth/concerns/user.rb +27 -36
- data/config/initializers/devise.rb +1 -1
- data/lib/devise_token_auth.rb +6 -6
- data/lib/devise_token_auth/controllers/helpers.rb +14 -4
- data/lib/devise_token_auth/engine.rb +11 -13
- data/lib/devise_token_auth/errors.rb +1 -1
- data/lib/devise_token_auth/rails/routes.rb +29 -29
- data/lib/devise_token_auth/url.rb +6 -4
- data/lib/devise_token_auth/version.rb +1 -1
- data/lib/generators/devise_token_auth/install_generator.rb +52 -39
- data/lib/generators/devise_token_auth/install_views_generator.rb +5 -5
- data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +1 -1
- data/lib/generators/devise_token_auth/templates/{user.rb → user.rb.erb} +0 -0
- data/test/controllers/custom/custom_confirmations_controller_test.rb +3 -1
- data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +2 -0
- data/test/controllers/custom/custom_passwords_controller_test.rb +4 -2
- data/test/controllers/custom/custom_registrations_controller_test.rb +6 -8
- data/test/controllers/custom/custom_sessions_controller_test.rb +5 -5
- data/test/controllers/custom/custom_token_validations_controller_test.rb +3 -3
- data/test/controllers/demo_group_controller_test.rb +2 -6
- data/test/controllers/demo_mang_controller_test.rb +2 -5
- data/test/controllers/demo_user_controller_test.rb +8 -17
- data/test/controllers/devise_token_auth/confirmations_controller_test.rb +2 -2
- data/test/controllers/devise_token_auth/passwords_controller_test.rb +13 -9
- data/test/controllers/devise_token_auth/registrations_controller_test.rb +8 -8
- data/test/controllers/devise_token_auth/sessions_controller_test.rb +22 -33
- data/test/controllers/devise_token_auth/token_validations_controller_test.rb +2 -6
- data/test/controllers/devise_token_auth/unlocks_controller_test.rb +1 -1
- data/test/controllers/overrides/confirmations_controller_test.rb +3 -1
- data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +3 -1
- data/test/controllers/overrides/passwords_controller_test.rb +25 -29
- data/test/controllers/overrides/registrations_controller_test.rb +4 -2
- data/test/controllers/overrides/sessions_controller_test.rb +4 -4
- data/test/controllers/overrides/token_validations_controller_test.rb +3 -3
- data/test/dummy/app/controllers/custom/confirmations_controller.rb +0 -2
- data/test/dummy/app/controllers/custom/passwords_controller.rb +1 -4
- data/test/dummy/app/controllers/custom/registrations_controller.rb +1 -2
- data/test/dummy/app/controllers/custom/sessions_controller.rb +1 -3
- data/test/dummy/app/controllers/custom/token_validations_controller.rb +1 -3
- data/test/dummy/app/controllers/overrides/confirmations_controller.rb +3 -3
- data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +3 -3
- data/test/dummy/app/controllers/overrides/passwords_controller.rb +5 -5
- data/test/dummy/app/controllers/overrides/registrations_controller.rb +2 -2
- data/test/dummy/app/controllers/overrides/sessions_controller.rb +8 -10
- data/test/dummy/app/controllers/overrides/token_validations_controller.rb +3 -5
- data/test/dummy/app/helpers/application_helper.rb +1027 -1036
- data/test/dummy/app/models/scoped_user.rb +2 -2
- data/test/dummy/app/models/unregisterable_user.rb +2 -2
- data/test/dummy/app/models/user.rb +1 -1
- data/test/dummy/config.ru +5 -3
- data/test/dummy/config/application.rb +2 -2
- data/test/dummy/config/boot.rb +2 -2
- data/test/dummy/config/environment.rb +1 -1
- data/test/dummy/config/environments/development.rb +3 -3
- data/test/dummy/config/environments/test.rb +1 -1
- data/test/dummy/config/initializers/omniauth.rb +2 -2
- data/test/dummy/config/routes.rb +10 -29
- data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +8 -8
- data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +7 -7
- data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +4 -4
- data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +7 -7
- data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +7 -7
- data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +7 -7
- data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +6 -6
- data/test/dummy/db/schema.rb +2 -60
- data/test/factories/users.rb +40 -0
- data/test/lib/devise_token_auth/url_test.rb +6 -6
- data/test/lib/generators/devise_token_auth/install_generator_test.rb +22 -14
- data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +30 -22
- data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +1 -1
- data/test/models/user_test.rb +15 -46
- data/test/support/controllers/routes.rb +43 -0
- data/test/test_helper.rb +12 -12
- metadata +8 -14
- data/test/dummy/app/models/evil_user.rb +0 -5
- data/test/dummy/app/models/nice_user.rb +0 -9
- data/test/dummy/db/migrate/20140928231203_devise_token_auth_create_evil_users.rb +0 -66
- data/test/dummy/db/migrate/20150409095712_devise_token_auth_create_nice_users.rb +0 -63
- data/test/integration/navigation_test.rb +0 -12
data/lib/devise_token_auth.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
3
|
+
require 'devise'
|
4
|
+
require 'devise_token_auth/engine'
|
5
|
+
require 'devise_token_auth/controllers/helpers'
|
6
|
+
require 'devise_token_auth/controllers/url_helpers'
|
7
|
+
require 'devise_token_auth/url'
|
8
|
+
require 'devise_token_auth/errors'
|
9
9
|
|
10
10
|
module DeviseTokenAuth
|
11
11
|
end
|
@@ -28,8 +28,8 @@ module DeviseTokenAuth
|
|
28
28
|
# before_action ->{ authenticate_blogger! :admin } # Redirects to the admin login page
|
29
29
|
# current_blogger :user # Preferably returns a User if one is signed in
|
30
30
|
#
|
31
|
-
def devise_token_auth_group(group_name, opts={})
|
32
|
-
mappings = "[#{
|
31
|
+
def devise_token_auth_group(group_name, opts = {})
|
32
|
+
mappings = "[#{opts[:contains].map { |m| ":#{m}" }.join(',')}]"
|
33
33
|
|
34
34
|
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
35
35
|
def authenticate_#{group_name}!(favourite=nil, opts={})
|
@@ -75,7 +75,12 @@ module DeviseTokenAuth
|
|
75
75
|
end
|
76
76
|
|
77
77
|
if respond_to?(:helper_method)
|
78
|
-
helper_method
|
78
|
+
helper_method(
|
79
|
+
"current_#{group_name}",
|
80
|
+
"current_#{group_name.to_s.pluralize}",
|
81
|
+
"#{group_name}_signed_in?",
|
82
|
+
"render_authenticate_error"
|
83
|
+
)
|
79
84
|
end
|
80
85
|
METHODS
|
81
86
|
end
|
@@ -142,7 +147,12 @@ module DeviseTokenAuth
|
|
142
147
|
|
143
148
|
ActiveSupport.on_load(:action_controller) do
|
144
149
|
if respond_to?(:helper_method)
|
145
|
-
helper_method
|
150
|
+
helper_method(
|
151
|
+
"current_#{mapping}",
|
152
|
+
"#{mapping}_signed_in?",
|
153
|
+
"#{mapping}_session",
|
154
|
+
'render_authenticate_error'
|
155
|
+
)
|
146
156
|
end
|
147
157
|
end
|
148
158
|
end
|
@@ -6,7 +6,7 @@ module DeviseTokenAuth
|
|
6
6
|
class Engine < ::Rails::Engine
|
7
7
|
isolate_namespace DeviseTokenAuth
|
8
8
|
|
9
|
-
initializer
|
9
|
+
initializer 'devise_token_auth.url_helpers' do
|
10
10
|
Devise.helpers << DeviseTokenAuth::Controllers::Helpers
|
11
11
|
end
|
12
12
|
end
|
@@ -38,11 +38,11 @@ module DeviseTokenAuth
|
|
38
38
|
self.enable_standard_devise_support = false
|
39
39
|
self.remove_tokens_after_password_reset = false
|
40
40
|
self.default_callbacks = true
|
41
|
-
self.headers_names = {
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
self.headers_names = { 'access-token': 'access-token',
|
42
|
+
'client': 'client',
|
43
|
+
'expiry': 'expiry',
|
44
|
+
'uid': 'uid',
|
45
|
+
'token-type': 'token-type' }
|
46
46
|
self.bypass_sign_in = true
|
47
47
|
|
48
48
|
def self.setup(&block)
|
@@ -50,23 +50,21 @@ module DeviseTokenAuth
|
|
50
50
|
|
51
51
|
Rails.application.config.after_initialize do
|
52
52
|
if defined?(::OmniAuth)
|
53
|
-
::OmniAuth::config.path_prefix = Devise.omniauth_path_prefix =
|
54
|
-
|
53
|
+
::OmniAuth::config.path_prefix = Devise.omniauth_path_prefix = omniauth_prefix
|
55
54
|
|
56
55
|
# Omniauth currently does not pass along omniauth.params upon failure redirect
|
57
56
|
# see also: https://github.com/intridea/omniauth/issues/626
|
58
57
|
OmniAuth::FailureEndpoint.class_eval do
|
59
58
|
def redirect_to_failure
|
60
59
|
message_key = env['omniauth.error.type']
|
61
|
-
origin_query_param = env['omniauth.origin'] ? "&origin=#{CGI.escape(env['omniauth.origin'])}" :
|
62
|
-
strategy_name_query_param = env['omniauth.error.strategy'] ? "&strategy=#{env['omniauth.error.strategy'].name}" :
|
63
|
-
extra_params = env['omniauth.params'] ? "&#{env['omniauth.params'].to_query}" :
|
60
|
+
origin_query_param = env['omniauth.origin'] ? "&origin=#{CGI.escape(env['omniauth.origin'])}" : ''
|
61
|
+
strategy_name_query_param = env['omniauth.error.strategy'] ? "&strategy=#{env['omniauth.error.strategy'].name}" : ''
|
62
|
+
extra_params = env['omniauth.params'] ? "&#{env['omniauth.params'].to_query}" : ''
|
64
63
|
new_path = "#{env['SCRIPT_NAME']}#{OmniAuth.config.path_prefix}/failure?message=#{message_key}#{origin_query_param}#{strategy_name_query_param}#{extra_params}"
|
65
|
-
Rack::Response.new([
|
64
|
+
Rack::Response.new(['302 Moved'], 302, 'Location' => new_path).finish
|
66
65
|
end
|
67
66
|
end
|
68
67
|
|
69
|
-
|
70
68
|
# Omniauth currently removes omniauth.params during mocked requests
|
71
69
|
# see also: https://github.com/intridea/omniauth/pull/812
|
72
70
|
OmniAuth::Strategy.class_eval do
|
@@ -8,31 +8,31 @@ module ActionDispatch::Routing
|
|
8
8
|
opts[:skip] ||= []
|
9
9
|
|
10
10
|
# check for ctrl overrides, fall back to defaults
|
11
|
-
sessions_ctrl = opts[:controllers][:sessions] ||
|
12
|
-
registrations_ctrl = opts[:controllers][:registrations] ||
|
13
|
-
passwords_ctrl = opts[:controllers][:passwords] ||
|
14
|
-
confirmations_ctrl = opts[:controllers][:confirmations] ||
|
15
|
-
token_validations_ctrl = opts[:controllers][:token_validations] ||
|
16
|
-
omniauth_ctrl = opts[:controllers][:omniauth_callbacks] ||
|
17
|
-
unlocks_ctrl = opts[:controllers][:unlocks] ||
|
11
|
+
sessions_ctrl = opts[:controllers][:sessions] || 'devise_token_auth/sessions'
|
12
|
+
registrations_ctrl = opts[:controllers][:registrations] || 'devise_token_auth/registrations'
|
13
|
+
passwords_ctrl = opts[:controllers][:passwords] || 'devise_token_auth/passwords'
|
14
|
+
confirmations_ctrl = opts[:controllers][:confirmations] || 'devise_token_auth/confirmations'
|
15
|
+
token_validations_ctrl = opts[:controllers][:token_validations] || 'devise_token_auth/token_validations'
|
16
|
+
omniauth_ctrl = opts[:controllers][:omniauth_callbacks] || 'devise_token_auth/omniauth_callbacks'
|
17
|
+
unlocks_ctrl = opts[:controllers][:unlocks] || 'devise_token_auth/unlocks'
|
18
18
|
|
19
19
|
# define devise controller mappings
|
20
|
-
controllers = {:
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
controllers = { sessions: sessions_ctrl,
|
21
|
+
registrations: registrations_ctrl,
|
22
|
+
passwords: passwords_ctrl,
|
23
|
+
confirmations: confirmations_ctrl }
|
24
24
|
|
25
25
|
controllers[:unlocks] = unlocks_ctrl if unlocks_ctrl
|
26
26
|
|
27
27
|
# remove any unwanted devise modules
|
28
|
-
opts[:skip].each{|item| controllers.delete(item)}
|
28
|
+
opts[:skip].each{ |item| controllers.delete(item) }
|
29
29
|
|
30
30
|
devise_for resource.pluralize.underscore.gsub('/', '_').to_sym,
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
class_name: resource,
|
32
|
+
module: :devise,
|
33
|
+
path: opts[:at].to_s,
|
34
|
+
controllers: controllers,
|
35
|
+
skip: opts[:skip] + [:omniauth_callbacks]
|
36
36
|
|
37
37
|
unnest_namespace do
|
38
38
|
# get full url path as if it were namespaced
|
@@ -43,8 +43,8 @@ module ActionDispatch::Routing
|
|
43
43
|
|
44
44
|
# clear scope so controller routes aren't namespaced
|
45
45
|
@scope = ActionDispatch::Routing::Mapper::Scope.new(
|
46
|
-
path:
|
47
|
-
shallow_path:
|
46
|
+
path: '',
|
47
|
+
shallow_path: '',
|
48
48
|
constraints: {},
|
49
49
|
defaults: {},
|
50
50
|
options: {},
|
@@ -56,29 +56,29 @@ module ActionDispatch::Routing
|
|
56
56
|
|
57
57
|
devise_scope mapping_name.to_sym do
|
58
58
|
# path to verify token validity
|
59
|
-
get "#{full_path}/validate_token", controller:
|
59
|
+
get "#{full_path}/validate_token", controller: token_validations_ctrl.to_s, action: 'validate_token'
|
60
60
|
|
61
61
|
# omniauth routes. only define if omniauth is installed and not skipped.
|
62
62
|
if defined?(::OmniAuth) && !opts[:skip].include?(:omniauth_callbacks)
|
63
|
-
match "#{full_path}/failure", controller: omniauth_ctrl, action:
|
64
|
-
match "#{full_path}/:provider/callback", controller: omniauth_ctrl, action:
|
63
|
+
match "#{full_path}/failure", controller: omniauth_ctrl, action: 'omniauth_failure', via: [:get]
|
64
|
+
match "#{full_path}/:provider/callback", controller: omniauth_ctrl, action: 'omniauth_success', via: [:get]
|
65
65
|
|
66
|
-
match "#{DeviseTokenAuth.omniauth_prefix}/:provider/callback", controller: omniauth_ctrl, action:
|
67
|
-
match "#{DeviseTokenAuth.omniauth_prefix}/failure", controller: omniauth_ctrl, action:
|
66
|
+
match "#{DeviseTokenAuth.omniauth_prefix}/:provider/callback", controller: omniauth_ctrl, action: 'redirect_callbacks', via: [:get, :post]
|
67
|
+
match "#{DeviseTokenAuth.omniauth_prefix}/failure", controller: omniauth_ctrl, action: 'omniauth_failure', via: [:get, :post]
|
68
68
|
|
69
69
|
# preserve the resource class thru oauth authentication by setting name of
|
70
70
|
# resource as "resource_class" param
|
71
|
-
match "#{full_path}/:provider", to: redirect{|params, request|
|
71
|
+
match "#{full_path}/:provider", to: redirect{ |params, request|
|
72
72
|
# get the current querystring
|
73
|
-
qs = CGI::parse(request.env[
|
73
|
+
qs = CGI::parse(request.env['QUERY_STRING'])
|
74
74
|
|
75
75
|
# append name of current resource
|
76
|
-
qs[
|
77
|
-
qs[
|
76
|
+
qs['resource_class'] = [resource]
|
77
|
+
qs['namespace_name'] = [namespace_name] if namespace_name
|
78
78
|
|
79
79
|
set_omniauth_path_prefix!(DeviseTokenAuth.omniauth_prefix)
|
80
80
|
|
81
|
-
redirect_params = {}.tap {|hash| qs.each{|k, v| hash[k] = v.first}}
|
81
|
+
redirect_params = {}.tap { |hash| qs.each{ |k, v| hash[k] = v.first } }
|
82
82
|
|
83
83
|
if DeviseTokenAuth.redirect_whitelist
|
84
84
|
redirect_url = request.params['auth_origin_url']
|
@@ -7,19 +7,21 @@ module DeviseTokenAuth::Url
|
|
7
7
|
|
8
8
|
res = "#{uri.scheme}://#{uri.host}"
|
9
9
|
res += ":#{uri.port}" if (uri.port && uri.port != 80 && uri.port != 443)
|
10
|
-
res +=
|
10
|
+
res += uri.path.to_s if uri.path
|
11
11
|
query = [uri.query, params.to_query].reject(&:blank?).join('&')
|
12
12
|
res += "?#{query}"
|
13
13
|
res += "##{uri.fragment}" if uri.fragment
|
14
14
|
|
15
|
-
|
15
|
+
res
|
16
16
|
end
|
17
17
|
|
18
18
|
def self.whitelisted?(url)
|
19
|
-
url.nil? ||
|
19
|
+
url.nil? || \
|
20
|
+
!!DeviseTokenAuth.redirect_whitelist.find do |pattern|
|
21
|
+
!!Wildcat.new(pattern).match(url)
|
22
|
+
end
|
20
23
|
end
|
21
24
|
|
22
|
-
|
23
25
|
# wildcard convenience class
|
24
26
|
class Wildcat
|
25
27
|
def self.parse_to_regex(str)
|
@@ -4,54 +4,56 @@ module DeviseTokenAuth
|
|
4
4
|
class InstallGenerator < Rails::Generators::Base
|
5
5
|
include Rails::Generators::Migration
|
6
6
|
|
7
|
-
|
7
|
+
class_option :primary_key_type, type: :string, desc: 'The type for primary key'
|
8
8
|
|
9
|
-
|
9
|
+
source_root File.expand_path('templates', __dir__)
|
10
|
+
|
11
|
+
argument :user_class, type: :string, default: 'User'
|
10
12
|
argument :mount_path, type: :string, default: 'auth'
|
11
13
|
|
12
14
|
def create_initializer_file
|
13
|
-
copy_file(
|
15
|
+
copy_file('devise_token_auth.rb', 'config/initializers/devise_token_auth.rb')
|
14
16
|
end
|
15
17
|
|
16
18
|
def copy_migrations
|
17
|
-
if self.class.migration_exists?(
|
18
|
-
say_status(
|
19
|
+
if self.class.migration_exists?('db/migrate', "devise_token_auth_create_#{user_class.pluralize.gsub('::','').underscore}")
|
20
|
+
say_status('skipped', "Migration 'devise_token_auth_create_#{user_class.pluralize.gsub('::','').underscore}' already exists")
|
19
21
|
else
|
20
22
|
migration_template(
|
21
|
-
|
22
|
-
"db/migrate/devise_token_auth_create_#{
|
23
|
+
'devise_token_auth_create_users.rb.erb',
|
24
|
+
"db/migrate/devise_token_auth_create_#{user_class.pluralize.gsub('::','').underscore}.rb"
|
23
25
|
)
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
27
29
|
def create_user_model
|
28
|
-
fname = "app/models/#{
|
29
|
-
|
30
|
-
|
31
|
-
else
|
32
|
-
inclusion = "include DeviseTokenAuth::Concerns::User"
|
30
|
+
fname = "app/models/#{user_class.underscore}.rb"
|
31
|
+
if File.exist?(File.join(destination_root, fname))
|
32
|
+
inclusion = 'include DeviseTokenAuth::Concerns::User'
|
33
33
|
unless parse_file_for_line(fname, inclusion)
|
34
34
|
|
35
35
|
active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
|
36
36
|
inject_into_file fname, after: "class #{user_class} < #{active_record_needle}\n" do <<-'RUBY'
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
37
|
+
# Include default devise modules.
|
38
|
+
devise :database_authenticatable, :registerable,
|
39
|
+
:recoverable, :rememberable, :trackable, :validatable,
|
40
|
+
:confirmable, :omniauthable
|
41
|
+
include DeviseTokenAuth::Concerns::User
|
42
|
+
RUBY
|
43
43
|
end
|
44
44
|
end
|
45
|
+
else
|
46
|
+
template('user.rb.erb', fname)
|
45
47
|
end
|
46
48
|
end
|
47
49
|
|
48
50
|
def include_controller_concerns
|
49
|
-
fname =
|
50
|
-
line =
|
51
|
+
fname = 'app/controllers/application_controller.rb'
|
52
|
+
line = 'include DeviseTokenAuth::Concerns::SetUserByToken'
|
51
53
|
|
52
54
|
if File.exist?(File.join(destination_root, fname))
|
53
55
|
if parse_file_for_line(fname, line)
|
54
|
-
say_status(
|
56
|
+
say_status('skipped', 'Concern is already included in the application controller.')
|
55
57
|
elsif is_rails_api?
|
56
58
|
inject_into_file fname, after: "class ApplicationController < ActionController::API\n" do <<-'RUBY'
|
57
59
|
include DeviseTokenAuth::Concerns::SetUserByToken
|
@@ -64,46 +66,46 @@ module DeviseTokenAuth
|
|
64
66
|
end
|
65
67
|
end
|
66
68
|
else
|
67
|
-
say_status(
|
69
|
+
say_status('skipped', "app/controllers/application_controller.rb not found. Add 'include DeviseTokenAuth::Concerns::SetUserByToken' to any controllers that require authentication.")
|
68
70
|
end
|
69
71
|
end
|
70
72
|
|
71
73
|
def add_route_mount
|
72
|
-
f =
|
74
|
+
f = 'config/routes.rb'
|
73
75
|
str = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"
|
74
76
|
|
75
77
|
if File.exist?(File.join(destination_root, f))
|
76
|
-
line = parse_file_for_line(f,
|
78
|
+
line = parse_file_for_line(f, 'mount_devise_token_auth_for')
|
77
79
|
|
78
|
-
|
79
|
-
line = "Rails.application.routes.draw do"
|
80
|
-
existing_user_class = false
|
81
|
-
else
|
80
|
+
if line
|
82
81
|
existing_user_class = true
|
82
|
+
else
|
83
|
+
line = 'Rails.application.routes.draw do'
|
84
|
+
existing_user_class = false
|
83
85
|
end
|
84
86
|
|
85
87
|
if parse_file_for_line(f, str)
|
86
|
-
say_status(
|
88
|
+
say_status('skipped', "Routes already exist for #{user_class} at #{mount_path}")
|
87
89
|
else
|
88
90
|
insert_after_line(f, line, str)
|
89
91
|
|
90
92
|
if existing_user_class
|
91
|
-
scoped_routes =
|
92
|
-
"as :#{user_class.underscore} do\n"
|
93
|
-
" # Define routes for #{user_class} within this block.\n"
|
93
|
+
scoped_routes = ''\
|
94
|
+
"as :#{user_class.underscore} do\n"\
|
95
|
+
" # Define routes for #{user_class} within this block.\n"\
|
94
96
|
" end\n"
|
95
97
|
insert_after_line(f, str, scoped_routes)
|
96
98
|
end
|
97
99
|
end
|
98
100
|
else
|
99
|
-
say_status(
|
101
|
+
say_status('skipped', "config/routes.rb not found. Add \"mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'\" to your routes file.")
|
100
102
|
end
|
101
103
|
end
|
102
104
|
|
103
105
|
private
|
104
106
|
|
105
107
|
def self.next_migration_number(path)
|
106
|
-
Time.zone.now.utc.strftime(
|
108
|
+
Time.zone.now.utc.strftime('%Y%m%d%H%M%S')
|
107
109
|
end
|
108
110
|
|
109
111
|
def insert_after_line(filename, line, str)
|
@@ -117,17 +119,15 @@ module DeviseTokenAuth
|
|
117
119
|
|
118
120
|
File.open(File.join(destination_root, filename)) do |f|
|
119
121
|
f.each_line do |line|
|
120
|
-
if line =~ /(#{Regexp.escape(str)})/mi
|
121
|
-
match = line
|
122
|
-
end
|
122
|
+
match = line if line =~ /(#{Regexp.escape(str)})/mi
|
123
123
|
end
|
124
124
|
end
|
125
125
|
match
|
126
126
|
end
|
127
127
|
|
128
128
|
def is_rails_api?
|
129
|
-
fname =
|
130
|
-
line =
|
129
|
+
fname = 'app/controllers/application_controller.rb'
|
130
|
+
line = 'class ApplicationController < ActionController::API'
|
131
131
|
parse_file_for_line(fname, line)
|
132
132
|
end
|
133
133
|
|
@@ -158,5 +158,18 @@ module DeviseTokenAuth
|
|
158
158
|
def database_version
|
159
159
|
ActiveRecord::Base.connection.select_value('SELECT VERSION()')
|
160
160
|
end
|
161
|
+
|
162
|
+
def rails5?
|
163
|
+
Rails.version.start_with? '5'
|
164
|
+
end
|
165
|
+
|
166
|
+
def primary_key_type
|
167
|
+
primary_key_string if rails5?
|
168
|
+
end
|
169
|
+
|
170
|
+
def primary_key_string
|
171
|
+
key_string = options[:primary_key_type]
|
172
|
+
", id: :#{key_string}" if key_string
|
173
|
+
end
|
161
174
|
end
|
162
175
|
end
|
@@ -2,16 +2,16 @@
|
|
2
2
|
|
3
3
|
module DeviseTokenAuth
|
4
4
|
class InstallViewsGenerator < Rails::Generators::Base
|
5
|
-
source_root File.expand_path('
|
5
|
+
source_root File.expand_path('../../../app/views/devise/mailer', __dir__)
|
6
6
|
|
7
7
|
def copy_mailer_templates
|
8
8
|
copy_file(
|
9
|
-
|
10
|
-
|
9
|
+
'confirmation_instructions.html.erb',
|
10
|
+
'app/views/devise/mailer/confirmation_instructions.html.erb'
|
11
11
|
)
|
12
12
|
copy_file(
|
13
|
-
|
14
|
-
|
13
|
+
'reset_password_instructions.html.erb',
|
14
|
+
'app/views/devise/mailer/reset_password_instructions.html.erb'
|
15
15
|
)
|
16
16
|
end
|
17
17
|
end
|