Module: DeviseTokenAuth::Concerns::ResourceFinder

Extended by:
ActiveSupport::Concern
Includes:
DeviseTokenAuth::Controllers::Helpers
Included in:
SetUserByToken
Defined in:
app/controllers/devise_token_auth/concerns/resource_finder.rb

Instance Method Summary collapse

Methods included from DeviseTokenAuth::Controllers::Helpers

define_helpers

Instance Method Details

#database_adapterObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/devise_token_auth/concerns/resource_finder.rb', line 32

def database_adapter
  @database_adapter ||= begin
    rails_version = [Rails::VERSION::MAJOR, Rails::VERSION::MINOR].join(".")

    adapter =
      if rails_version >= "6.1"
        resource_class.try(:connection_db_config)&.try(:adapter)
      else
        resource_class.try(:connection_config)&.try(:[], :adapter)
      end
  end
end

#find_resource(field, value) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'app/controllers/devise_token_auth/concerns/resource_finder.rb', line 22

def find_resource(field, value)
  @resource = if database_adapter&.include?('mysql')
                # fix for mysql default case insensitivity
                field_sanitized = resource_class.connection.quote_column_name(field)
                resource_class.where("BINARY #{field_sanitized} = ? AND provider= ?", value, provider).first
              else
                resource_class.dta_find_by(field => value, 'provider' => provider)
              end
end

#get_case_insensitive_field_from_resource_params(field) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/devise_token_auth/concerns/resource_finder.rb', line 7

def get_case_insensitive_field_from_resource_params(field)
  # honor Devise configuration for case_insensitive keys
  q_value = resource_params[field.to_sym]

  if resource_class.case_insensitive_keys.include?(field.to_sym)
    q_value.downcase!
  end

  if resource_class.strip_whitespace_keys.include?(field.to_sym)
    q_value.strip!
  end

  q_value
end

#providerObject



55
56
57
# File 'app/controllers/devise_token_auth/concerns/resource_finder.rb', line 55

def provider
  'email'
end

#resource_class(m = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'app/controllers/devise_token_auth/concerns/resource_finder.rb', line 45

def resource_class(m = nil)
  mapping = if m
              Devise.mappings[m]
            else
              Devise.mappings[resource_name] || Devise.mappings.values.first
            end

  mapping.to
end