devise_jwt_auth 0.1.1 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/app/controllers/devise_jwt_auth/application_controller.rb +15 -13
  4. data/app/controllers/devise_jwt_auth/concerns/resource_finder.rb +2 -6
  5. data/app/controllers/devise_jwt_auth/concerns/{set_user_by_jwt_token.rb → set_user_by_token.rb} +23 -19
  6. data/app/controllers/devise_jwt_auth/confirmations_controller.rb +10 -19
  7. data/app/controllers/devise_jwt_auth/omniauth_callbacks_controller.rb +32 -33
  8. data/app/controllers/devise_jwt_auth/passwords_controller.rb +29 -19
  9. data/app/controllers/devise_jwt_auth/refresh_token_controller.rb +4 -1
  10. data/app/controllers/devise_jwt_auth/registrations_controller.rb +40 -21
  11. data/app/controllers/devise_jwt_auth/sessions_controller.rb +21 -21
  12. data/app/controllers/devise_jwt_auth/unlocks_controller.rb +5 -4
  13. data/app/models/devise_jwt_auth/concerns/active_record_support.rb +3 -0
  14. data/app/models/devise_jwt_auth/concerns/confirmable_support.rb +7 -4
  15. data/app/models/devise_jwt_auth/concerns/mongoid_support.rb +3 -0
  16. data/app/models/devise_jwt_auth/concerns/tokens_serialization.rb +4 -1
  17. data/app/models/devise_jwt_auth/concerns/user.rb +18 -9
  18. data/app/models/devise_jwt_auth/concerns/user_omniauth_callbacks.rb +11 -3
  19. data/app/validators/devise_jwt_auth_email_validator.rb +5 -4
  20. data/lib/devise_jwt_auth/blacklist.rb +2 -0
  21. data/lib/devise_jwt_auth/controllers/url_helpers.rb +1 -2
  22. data/lib/devise_jwt_auth/engine.rb +4 -4
  23. data/lib/devise_jwt_auth/rails/routes.rb +35 -24
  24. data/lib/devise_jwt_auth/token_factory.rb +3 -2
  25. data/lib/devise_jwt_auth/url.rb +2 -4
  26. data/lib/devise_jwt_auth/version.rb +1 -1
  27. data/lib/generators/devise_jwt_auth/install_generator.rb +7 -6
  28. data/lib/generators/devise_jwt_auth/install_generator_helpers.rb +27 -6
  29. data/lib/generators/devise_jwt_auth/install_mongoid_generator.rb +3 -2
  30. data/lib/generators/devise_jwt_auth/templates/devise_jwt_auth.rb +15 -16
  31. data/lib/generators/devise_jwt_auth/templates/devise_jwt_auth_create_users.rb.erb +15 -11
  32. data/lib/generators/devise_jwt_auth/templates/user.rb.erb +2 -2
  33. data/test/controllers/custom/custom_confirmations_controller_test.rb +2 -2
  34. data/test/controllers/custom/custom_passwords_controller_test.rb +4 -4
  35. data/test/controllers/custom/custom_refresh_token_controller_test.rb +2 -3
  36. data/test/controllers/custom/custom_registrations_controller_test.rb +2 -2
  37. data/test/controllers/demo_mang_controller_test.rb +206 -210
  38. data/test/controllers/demo_user_controller_test.rb +358 -374
  39. data/test/controllers/devise_jwt_auth/confirmations_controller_test.rb +5 -5
  40. data/test/controllers/devise_jwt_auth/omniauth_callbacks_controller_test.rb +6 -7
  41. data/test/controllers/devise_jwt_auth/passwords_controller_test.rb +11 -13
  42. data/test/controllers/devise_jwt_auth/refresh_token_controller_test.rb +8 -12
  43. data/test/controllers/devise_jwt_auth/registrations_controller_test.rb +23 -25
  44. data/test/controllers/devise_jwt_auth/sessions_controller_test.rb +32 -34
  45. data/test/controllers/devise_jwt_auth/unlocks_controller_test.rb +2 -2
  46. data/test/controllers/overrides/confirmations_controller_test.rb +1 -1
  47. data/test/controllers/overrides/passwords_controller_test.rb +1 -1
  48. data/test/controllers/overrides/refresh_token_controller_test.rb +1 -2
  49. data/test/controllers/overrides/registrations_controller_test.rb +1 -1
  50. data/test/dummy/app/controllers/application_controller.rb +1 -1
  51. data/test/dummy/app/controllers/custom/refresh_token_controller.rb +2 -1
  52. data/test/dummy/app/controllers/custom/registrations_controller.rb +1 -1
  53. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +4 -4
  54. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +4 -4
  55. data/test/dummy/app/controllers/overrides/passwords_controller.rb +4 -4
  56. data/test/dummy/app/controllers/overrides/refresh_token_controller.rb +1 -1
  57. data/test/dummy/app/controllers/overrides/registrations_controller.rb +2 -2
  58. data/test/dummy/app/controllers/overrides/sessions_controller.rb +2 -2
  59. data/test/dummy/app/models/concerns/favorite_color.rb +11 -9
  60. data/test/dummy/config.ru +2 -2
  61. data/test/dummy/config/application.rb +1 -0
  62. data/test/dummy/config/boot.rb +1 -1
  63. data/test/dummy/config/environments/test.rb +11 -7
  64. data/test/dummy/config/initializers/figaro.rb +1 -1
  65. data/test/dummy/config/initializers/omniauth.rb +2 -2
  66. data/test/dummy/config/routes.rb +8 -8
  67. data/test/dummy/db/migrate/{20140715061447_devise_token_auth_create_users.rb → 20140715061447_devise_jwt_auth_create_users.rb} +0 -0
  68. data/test/dummy/db/migrate/{20140715061805_devise_token_auth_create_mangs.rb → 20140715061805_devise_jwt_auth_create_mangs.rb} +0 -0
  69. data/test/dummy/db/migrate/{20141222035835_devise_token_auth_create_only_email_users.rb → 20141222035835_devise_jwt_auth_create_only_email_users.rb} +9 -9
  70. data/test/dummy/db/migrate/{20141222053502_devise_token_auth_create_unregisterable_users.rb → 20141222053502_devise_jwt_auth_create_unregisterable_users.rb} +0 -0
  71. data/test/dummy/db/migrate/{20150708104536_devise_token_auth_create_unconfirmable_users.rb → 20150708104536_devise_jwt_auth_create_unconfirmable_users.rb} +0 -0
  72. data/test/dummy/db/migrate/{20160103235141_devise_token_auth_create_scoped_users.rb → 20160103235141_devise_jwt_auth_create_scoped_users.rb} +0 -0
  73. data/test/dummy/db/migrate/{20160629184441_devise_token_auth_create_lockable_users.rb → 20160629184441_devise_jwt_auth_create_lockable_users.rb} +0 -0
  74. data/test/dummy/db/migrate/{20190924101113_devise_token_auth_create_confirmable_users.rb → 20190924101113_devise_jwt_auth_create_confirmable_users.rb} +6 -5
  75. data/test/dummy/db/schema.rb +170 -170
  76. data/test/dummy/tmp/generators/app/controllers/application_controller.rb +6 -0
  77. data/test/dummy/tmp/generators/app/models/user.rb +9 -0
  78. data/test/dummy/tmp/generators/config/initializers/devise_jwt_auth.rb +73 -0
  79. data/test/dummy/tmp/generators/db/migrate/20201006030349_devise_jwt_auth_create_users.rb +54 -0
  80. data/test/factories/users.rb +5 -3
  81. data/test/lib/devise_jwt_auth/token_factory_test.rb +6 -6
  82. data/test/lib/generators/devise_jwt_auth/install_generator_test.rb +3 -20
  83. data/test/lib/generators/devise_jwt_auth/install_generator_with_namespace_test.rb +4 -21
  84. data/test/models/concerns/tokens_serialization_test.rb +68 -68
  85. data/test/models/user_test.rb +35 -37
  86. data/test/support/controllers/routes.rb +7 -5
  87. data/test/test_helper.rb +1 -1
  88. metadata +70 -76
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # ActiveSupport Concern for Mongoid support
1
4
  module DeviseJwtAuth::Concerns::MongoidSupport
2
5
  extend ActiveSupport::Concern
3
6
 
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Dumping and loading serialized tokens in JSON format.
1
4
  module DeviseJwtAuth::Concerns::TokensSerialization
2
5
  # Serialization hash to json
3
6
  def self.dump(object)
4
- object.each_value(&:compact!) unless object.nil?
7
+ object&.each_value(&:compact!)
5
8
  JSON.generate(object)
6
9
  end
7
10
 
@@ -22,14 +22,20 @@ module DeviseJwtAuth::Concerns::User
22
22
  include DeviseJwtAuth::Concerns::MongoidSupport
23
23
  end
24
24
 
25
- if DeviseJwtAuth.default_callbacks
26
- include DeviseJwtAuth::Concerns::UserOmniauthCallbacks
27
- end
25
+ include DeviseJwtAuth::Concerns::UserOmniauthCallbacks if DeviseJwtAuth.default_callbacks
28
26
 
29
27
  # don't use default devise email validation
30
- def email_required?; false; end
31
- def email_changed?; false; end
32
- def will_save_change_to_email?; false; end
28
+ def email_required?
29
+ false
30
+ end
31
+
32
+ def email_changed?
33
+ false
34
+ end
35
+
36
+ def will_save_change_to_email?
37
+ false
38
+ end
33
39
 
34
40
  if DeviseJwtAuth.send_confirmation_email && devise_modules.include?(:confirmable)
35
41
  include DeviseJwtAuth::Concerns::ConfirmableSupport
@@ -37,6 +43,7 @@ module DeviseJwtAuth::Concerns::User
37
43
 
38
44
  def password_required?
39
45
  return false unless provider == 'email'
46
+
40
47
  super
41
48
  end
42
49
 
@@ -77,11 +84,11 @@ module DeviseJwtAuth::Concerns::User
77
84
  end
78
85
 
79
86
  def create_token(token_options = {})
80
- DeviseJwtAuth::TokenFactory.create_access_token({sub: uid}.merge(token_options))
87
+ DeviseJwtAuth::TokenFactory.create_access_token({ sub: uid }.merge(token_options))
81
88
  end
82
89
 
83
90
  def create_refresh_token(token_options = {})
84
- DeviseJwtAuth::TokenFactory.create_refresh_token({sub: uid}.merge(token_options))
91
+ DeviseJwtAuth::TokenFactory.create_refresh_token({ sub: uid }.merge(token_options))
85
92
  end
86
93
  end
87
94
 
@@ -93,7 +100,9 @@ module DeviseJwtAuth::Concerns::User
93
100
 
94
101
  # this must be done from the controller so that additional params
95
102
  # can be passed on from the client
96
- def send_confirmation_notification?; false; end
103
+ def send_confirmation_notification?
104
+ false
105
+ end
97
106
 
98
107
  def build_auth_url(base_url, args)
99
108
  args[:uid] = uid
@@ -4,12 +4,20 @@ module DeviseJwtAuth::Concerns::UserOmniauthCallbacks
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
- validates :email, presence: true,if: :email_provider?
8
- validates :email, :devise_jwt_auth_email => true, allow_nil: true, allow_blank: true, if: :email_provider?
7
+ validates :email, presence: true, if: :email_provider?
8
+ validates :email,
9
+ devise_jwt_auth_email: true,
10
+ allow_nil: true,
11
+ allow_blank: true,
12
+ if: :email_provider?
13
+
9
14
  validates_presence_of :uid, unless: :email_provider?
10
15
 
11
16
  # only validate unique emails among email registration users
12
- validates :email, uniqueness: { case_sensitive: false, scope: :provider }, on: :create, if: :email_provider?
17
+ validates :email,
18
+ uniqueness: { case_sensitive: false, scope: :provider },
19
+ on: :create,
20
+ if: :email_provider?
13
21
 
14
22
  # keep uid in sync with email
15
23
  before_save :sync_uid
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Email field validator.
3
4
  class DeviseJwtAuthEmailValidator < ActiveModel::EachValidator
4
5
  def validate_each(record, attribute, value)
5
- unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
6
- record.errors[attribute] << email_invalid_message
7
- end
6
+ return if value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
7
+
8
+ record.errors[attribute] << email_invalid_message
8
9
  end
9
10
 
10
11
  private
@@ -14,7 +15,7 @@ class DeviseJwtAuthEmailValidator < ActiveModel::EachValidator
14
15
  message = options[:message]
15
16
 
16
17
  if message.nil?
17
- # Try DeviceTokenAuth translations or fallback to ActiveModel translations
18
+ # Try DeviseJwtAuth translations or fallback to ActiveModel translations
18
19
  message = I18n.t(:'errors.messages.not_email', default: :'errors.messages.invalid')
19
20
  end
20
21
 
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # don't serialize tokens
2
4
  Devise::Models::Authenticatable::BLACKLIST_FOR_SERIALIZATION << :tokens
@@ -3,8 +3,7 @@
3
3
  module DeviseJwtAuth
4
4
  module Controllers
5
5
  module UrlHelpers
6
- def self.define_helpers(mapping)
7
- end
6
+ def self.define_helpers(mapping); end
8
7
  end
9
8
  end
10
9
  end
@@ -33,7 +33,7 @@ module DeviseJwtAuth
33
33
 
34
34
  self.send_new_access_token_on_each_request = false
35
35
  self.refresh_token_lifespan = 1.week
36
- self.access_token_lifespan = 1.day
36
+ self.access_token_lifespan = 15.minutes
37
37
  self.refresh_token_name = 'refresh-token'
38
38
  self.access_token_name = 'access-token'
39
39
  self.refresh_token_encryption_key = 'your-refresh-token-secret-key-here'
@@ -51,12 +51,12 @@ module DeviseJwtAuth
51
51
  self.send_confirmation_email = false
52
52
  self.require_client_password_reset_token = false
53
53
 
54
- def self.setup(&block)
54
+ def self.setup
55
55
  yield self
56
56
 
57
57
  Rails.application.config.after_initialize do
58
58
  if defined?(::OmniAuth)
59
- ::OmniAuth::config.path_prefix = Devise.omniauth_path_prefix = omniauth_prefix
59
+ ::OmniAuth.config.path_prefix = Devise.omniauth_path_prefix = omniauth_prefix
60
60
 
61
61
  # Omniauth currently does not pass along omniauth.params upon failure redirect
62
62
  # see also: https://github.com/intridea/omniauth/issues/626
@@ -84,7 +84,7 @@ module DeviseJwtAuth
84
84
  fail!(mocked_auth)
85
85
  else
86
86
  @env['omniauth.auth'] = mocked_auth
87
- OmniAuth.config.before_callback_phase.call(@env) if OmniAuth.config.before_callback_phase
87
+ OmniAuth.config.before_callback_phase&.call(@env)
88
88
  call_app!
89
89
  end
90
90
  end
@@ -8,14 +8,13 @@ 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] || 'devise_jwt_auth/sessions'
12
- registrations_ctrl = opts[:controllers][:registrations] || 'devise_jwt_auth/registrations'
13
- passwords_ctrl = opts[:controllers][:passwords] || 'devise_jwt_auth/passwords'
14
- confirmations_ctrl = opts[:controllers][:confirmations] || 'devise_jwt_auth/confirmations'
15
- # token_validations_ctrl = opts[:controllers][:token_validations] || 'devise_jwt_auth/token_validations'
16
- refresh_token_ctrl = opts[:controllers][:refresh_token] || 'devise_jwt_auth/refresh_token'
17
- omniauth_ctrl = opts[:controllers][:omniauth_callbacks] || 'devise_jwt_auth/omniauth_callbacks'
18
- unlocks_ctrl = opts[:controllers][:unlocks] || 'devise_jwt_auth/unlocks'
11
+ sessions_ctrl = opts[:controllers][:sessions] || 'devise_jwt_auth/sessions'
12
+ registrations_ctrl = opts[:controllers][:registrations] || 'devise_jwt_auth/registrations'
13
+ passwords_ctrl = opts[:controllers][:passwords] || 'devise_jwt_auth/passwords'
14
+ confirmations_ctrl = opts[:controllers][:confirmations] || 'devise_jwt_auth/confirmations'
15
+ refresh_token_ctrl = opts[:controllers][:refresh_token] || 'devise_jwt_auth/refresh_token'
16
+ omniauth_ctrl = opts[:controllers][:omniauth_callbacks] || 'devise_jwt_auth/omniauth_callbacks'
17
+ unlocks_ctrl = opts[:controllers][:unlocks] || 'devise_jwt_auth/unlocks'
19
18
 
20
19
  # define devise controller mappings
21
20
  controllers = { sessions: sessions_ctrl,
@@ -26,7 +25,7 @@ module ActionDispatch::Routing
26
25
  controllers[:unlocks] = unlocks_ctrl if unlocks_ctrl
27
26
 
28
27
  # remove any unwanted devise modules
29
- opts[:skip].each{ |item| controllers.delete(item) }
28
+ opts[:skip].each { |item| controllers.delete(item) }
30
29
 
31
30
  devise_for resource.pluralize.underscore.gsub('/', '_').to_sym,
32
31
  class_name: resource,
@@ -44,12 +43,12 @@ module ActionDispatch::Routing
44
43
 
45
44
  # clear scope so controller routes aren't namespaced
46
45
  @scope = ActionDispatch::Routing::Mapper::Scope.new(
47
- path: '',
46
+ path: '',
48
47
  shallow_path: '',
49
- constraints: {},
50
- defaults: {},
51
- options: {},
52
- parent: nil
48
+ constraints: {},
49
+ defaults: {},
50
+ options: {},
51
+ parent: nil
53
52
  )
54
53
 
55
54
  mapping_name = resource.underscore.gsub('/', '_')
@@ -57,22 +56,34 @@ module ActionDispatch::Routing
57
56
 
58
57
  devise_scope mapping_name.to_sym do
59
58
  # path to refresh access tokens
60
- get "#{full_path}/refresh_token", controller: refresh_token_ctrl.to_s, action: 'show' if !opts[:skip].include?(:refresh_token)
61
- # get "#{full_path}/validate_token", controller: token_validations_ctrl.to_s, action: 'validate_token' if !opts[:skip].include?(:token_validations)
59
+ unless opts[:skip].include?(:refresh_token)
60
+ get "#{full_path}/refresh_token", controller: refresh_token_ctrl.to_s, action: 'show'
61
+ end
62
62
 
63
63
  # omniauth routes. only define if omniauth is installed and not skipped.
64
64
  if defined?(::OmniAuth) && !opts[:skip].include?(:omniauth_callbacks)
65
- match "#{full_path}/failure", controller: omniauth_ctrl, action: 'omniauth_failure', via: [:get]
66
- match "#{full_path}/:provider/callback", controller: omniauth_ctrl, action: 'omniauth_success', via: [:get]
67
-
68
- match "#{DeviseJwtAuth.omniauth_prefix}/:provider/callback", controller: omniauth_ctrl, action: 'redirect_callbacks', via: [:get, :post]
69
- match "#{DeviseJwtAuth.omniauth_prefix}/failure", controller: omniauth_ctrl, action: 'omniauth_failure', via: [:get, :post]
65
+ match "#{full_path}/failure",
66
+ controller: omniauth_ctrl,
67
+ action: 'omniauth_failure',
68
+ via: [:get]
69
+ match "#{full_path}/:provider/callback",
70
+ controller: omniauth_ctrl,
71
+ action: 'omniauth_success',
72
+ via: [:get]
73
+ match "#{DeviseJwtAuth.omniauth_prefix}/:provider/callback",
74
+ controller: omniauth_ctrl,
75
+ action: 'redirect_callbacks',
76
+ via: [:get, :post]
77
+ match "#{DeviseJwtAuth.omniauth_prefix}/failure",
78
+ controller: omniauth_ctrl,
79
+ action: 'omniauth_failure',
80
+ via: [:get, :post]
70
81
 
71
82
  # preserve the resource class thru oauth authentication by setting name of
72
83
  # resource as "resource_class" param
73
- match "#{full_path}/:provider", to: redirect{ |params, request|
84
+ match "#{full_path}/:provider", to: redirect { |params, request|
74
85
  # get the current querystring
75
- qs = CGI::parse(request.env['QUERY_STRING'])
86
+ qs = CGI.parse(request.env['QUERY_STRING'])
76
87
 
77
88
  # append name of current resource
78
89
  qs['resource_class'] = [resource]
@@ -80,7 +91,7 @@ module ActionDispatch::Routing
80
91
 
81
92
  set_omniauth_path_prefix!(DeviseJwtAuth.omniauth_prefix)
82
93
 
83
- redirect_params = {}.tap { |hash| qs.each{ |k, v| hash[k] = v.first } }
94
+ redirect_params = {}.tap { |hash| qs.each { |k, v| hash[k] = v.first } }
84
95
 
85
96
  if DeviseJwtAuth.redirect_whitelist
86
97
  redirect_url = request.params['auth_origin_url']
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'jwt'
2
4
 
3
5
  module DeviseJwtAuth
4
6
  # A token management factory which allow generate token objects and check them.
5
7
  module TokenFactory
6
-
7
8
  def self.create_refresh_token(payload)
8
9
  if payload[:exp].blank? && payload['exp'].blank?
9
10
  payload[:exp] = (Time.zone.now + DeviseJwtAuth.refresh_token_lifespan).to_i
@@ -33,7 +34,7 @@ module DeviseJwtAuth
33
34
  rescue TypeError
34
35
  {}
35
36
  end
36
-
37
+
37
38
  def self.decode_access_token(token)
38
39
  JWT.decode(token, DeviseJwtAuth.access_token_encryption_key).first
39
40
  rescue JWT::ExpiredSignature
@@ -1,12 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeviseJwtAuth::Url
4
-
5
4
  def self.generate(url, params = {})
6
5
  uri = URI(url)
7
6
 
8
7
  res = "#{uri.scheme}://#{uri.host}"
9
- res += ":#{uri.port}" if (uri.port && uri.port != 80 && uri.port != 443)
8
+ res += ":#{uri.port}" if uri.port && uri.port != 80 && uri.port != 443
10
9
  res += uri.path.to_s if uri.path
11
10
  query = [uri.query, params.to_query].reject(&:blank?).join('&')
12
11
  res += "?#{query}"
@@ -28,7 +27,7 @@ module DeviseJwtAuth::Url
28
27
  # wildcard convenience class
29
28
  class Wildcat
30
29
  def self.parse_to_regex(str)
31
- escaped = Regexp.escape(str).gsub('\*','.*?')
30
+ escaped = Regexp.escape(str).gsub('\*', '.*?')
32
31
  Regexp.new("^#{escaped}$", Regexp::IGNORECASE)
33
32
  end
34
33
 
@@ -40,5 +39,4 @@ module DeviseJwtAuth::Url
40
39
  !!@regex.match(str)
41
40
  end
42
41
  end
43
-
44
42
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeviseJwtAuth
4
- VERSION = '0.1.1'.freeze
4
+ VERSION = '0.1.6'
5
5
  end
@@ -10,12 +10,12 @@ module DeviseJwtAuth
10
10
  class_option :primary_key_type, type: :string, desc: 'The type for primary key'
11
11
 
12
12
  def copy_migrations
13
- if self.class.migration_exists?('db/migrate', "devise_jwt_auth_create_#{user_class.pluralize.gsub('::','').underscore}")
14
- say_status('skipped', "Migration 'devise_jwt_auth_create_#{user_class.pluralize.gsub('::','').underscore}' already exists")
13
+ if self.class.migration_exists?('db/migrate', "devise_jwt_auth_create_#{user_class.pluralize.gsub('::', '').underscore}")
14
+ say_status('skipped', "Migration 'devise_jwt_auth_create_#{user_class.pluralize.gsub('::', '').underscore}' already exists")
15
15
  else
16
16
  migration_template(
17
17
  'devise_jwt_auth_create_users.rb.erb',
18
- "db/migrate/devise_jwt_auth_create_#{user_class.pluralize.gsub('::','').underscore}.rb"
18
+ "db/migrate/devise_jwt_auth_create_#{user_class.pluralize.gsub('::', '').underscore}.rb"
19
19
  )
20
20
  end
21
21
  end
@@ -26,8 +26,9 @@ module DeviseJwtAuth
26
26
  inclusion = 'include DeviseJwtAuth::Concerns::User'
27
27
  unless parse_file_for_line(fname, inclusion)
28
28
 
29
- active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
30
- inject_into_file fname, after: "class #{user_class} < #{active_record_needle}\n" do <<-'RUBY'
29
+ active_record_needle = Rails::VERSION::MAJOR == 5 ? 'ApplicationRecord' : 'ActiveRecord::Base'
30
+ inject_into_file fname, after: "class #{user_class} < #{active_record_needle}\n" do
31
+ <<-'RUBY'
31
32
  # Include default devise modules.
32
33
  devise :database_authenticatable, :registerable,
33
34
  :recoverable, :rememberable, :trackable, :validatable,
@@ -43,7 +44,7 @@ module DeviseJwtAuth
43
44
 
44
45
  private
45
46
 
46
- def self.next_migration_number(path)
47
+ def self.next_migration_number(_path)
47
48
  Time.zone.now.utc.strftime('%Y%m%d%H%M%S')
48
49
  end
49
50
 
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeviseJwtAuth
4
+ # Helper methods for installation generators.
2
5
  module InstallGeneratorHelpers
3
6
  class << self
4
7
  def included(mod)
@@ -19,15 +22,19 @@ module DeviseJwtAuth
19
22
  if File.exist?(File.join(destination_root, fname))
20
23
  if parse_file_for_line(fname, line)
21
24
  say_status('skipped', 'Concern is already included in the application controller.')
22
- elsif is_rails_api?
23
- inject_into_file fname, after: "class ApplicationController < ActionController::API\n" do <<-'RUBY'
25
+ elsif rails_api?
26
+ inject_into_file fname,
27
+ after: "class ApplicationController < ActionController::API\n" do
28
+ <<-'RUBY'
24
29
  include DeviseJwtAuth::Concerns::SetUserByToken
25
- RUBY
30
+ RUBY
26
31
  end
27
32
  else
28
- inject_into_file fname, after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY'
33
+ inject_into_file fname,
34
+ after: "class ApplicationController < ActionController::Base\n" do
35
+ <<-'RUBY'
29
36
  include DeviseJwtAuth::Concerns::SetUserByToken
30
- RUBY
37
+ RUBY
31
38
  end
32
39
  end
33
40
  else
@@ -67,6 +74,20 @@ module DeviseJwtAuth
67
74
  end
68
75
  end
69
76
 
77
+ def ip_column
78
+ # Padded with spaces so it aligns nicely with the rest of the columns.
79
+ format('%-8s', (inet? ? 'inet' : 'string'))
80
+ end
81
+
82
+ def inet?
83
+ postgresql?
84
+ end
85
+
86
+ def postgresql?
87
+ config = ActiveRecord::Base.configurations[Rails.env]
88
+ config && config['adapter'] == 'postgresql'
89
+ end
90
+
70
91
  private
71
92
 
72
93
  def insert_after_line(filename, line, str)
@@ -86,7 +107,7 @@ module DeviseJwtAuth
86
107
  match
87
108
  end
88
109
 
89
- def is_rails_api?
110
+ def rails_api?
90
111
  fname = 'app/controllers/application_controller.rb'
91
112
  line = 'class ApplicationController < ActionController::API'
92
113
  parse_file_for_line(fname, line)
@@ -3,6 +3,7 @@
3
3
  require_relative 'install_generator_helpers'
4
4
 
5
5
  module DeviseJwtAuth
6
+ # Adds Mongoid settings to ORM
6
7
  class InstallMongoidGenerator < Rails::Generators::Base
7
8
  include DeviseJwtAuth::InstallGeneratorHelpers
8
9
 
@@ -11,8 +12,8 @@ module DeviseJwtAuth
11
12
  if File.exist?(File.join(destination_root, fname))
12
13
  inclusion = 'include DeviseJwtAuth::Concerns::User'
13
14
  unless parse_file_for_line(fname, inclusion)
14
- inject_into_file fname, before: /end\s\z/ do <<-'RUBY'
15
-
15
+ inject_into_file fname, before: /end\s\z/ do
16
+ <<-'RUBY'
16
17
  include Mongoid::Locker
17
18
 
18
19
  field :locker_locked_at, type: Time