Skip to content

Commit

Permalink
Support authentication with multiple fields
Browse files Browse the repository at this point in the history
  • Loading branch information
danielneis committed Nov 6, 2017
1 parent b86b217 commit 567f792
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
24 changes: 18 additions & 6 deletions app/controllers/devise_token_auth/concerns/resource_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@ def get_case_insensitive_field_from_resource_params(field)
q_value
end

def find_resource(field, value)
# fix for mysql default case insensitivity
q = "#{field.to_s} = ? AND provider='#{provider.to_s}'"
if ActiveRecord::Base.connection.adapter_name.downcase.starts_with? 'mysql'
q = "BINARY " + q
def find_resource

fields = (resource_params.keys.map(&:to_sym) & resource_class.authentication_keys)

conditions = []
values = {}
fields.each do |f|
q = " #{f.to_s} = :#{f.to_s} "
# fix for mysql default case insensitivity
if ActiveRecord::Base.connection.adapter_name.downcase.starts_with? 'mysql'
q = "BINARY " + q
end
conditions.push(q)
values[f.to_sym] = get_case_insensitive_field_from_resource_params(f)
end

@resource = resource_class.where(q, value).first
conditions.push(' provider = :provider')
values[:provider] = provider.to_s

@resource = resource_class.where([conditions.join(" AND "), values]).first
end

def resource_class(m=nil)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/devise_token_auth/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def create
end

@email = get_case_insensitive_field_from_resource_params(:email)
@resource = find_resource(:uid, @email)
@resource = find_resource

@errors = nil
@error_status = 400
Expand Down
10 changes: 2 additions & 8 deletions app/controllers/devise_token_auth/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@ def new

def create
# Check
field = (resource_params.keys.map(&:to_sym) & resource_class.authentication_keys).first

@resource = nil
if field
q_value = get_case_insensitive_field_from_resource_params(field)
@resource = find_resource

@resource = find_resource(field, q_value)
end

if @resource && valid_params?(field, q_value) && (!@resource.respond_to?(:active_for_authentication?) || @resource.active_for_authentication?)
if @resource && (!@resource.respond_to?(:active_for_authentication?) || @resource.active_for_authentication?)
valid_password = @resource.valid_password?(resource_params[:password])
if (@resource.respond_to?(:valid_for_authentication?) && !@resource.valid_for_authentication? { valid_password }) || !valid_password
render_create_error_bad_credentials
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/devise_token_auth/unlocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def create
end

@email = get_case_insensitive_field_from_resource_params(:email)
@resource = find_resource(:email, @email)
@resource = find_resource

@errors = nil
@error_status = 400
Expand Down

0 comments on commit 567f792

Please sign in to comment.