Skip to content

Commit 567f792

Browse files
committed
Support authentication with multiple fields
1 parent b86b217 commit 567f792

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

app/controllers/devise_token_auth/concerns/resource_finder.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,26 @@ def get_case_insensitive_field_from_resource_params(field)
1212
q_value
1313
end
1414

15-
def find_resource(field, value)
16-
# fix for mysql default case insensitivity
17-
q = "#{field.to_s} = ? AND provider='#{provider.to_s}'"
18-
if ActiveRecord::Base.connection.adapter_name.downcase.starts_with? 'mysql'
19-
q = "BINARY " + q
15+
def find_resource
16+
17+
fields = (resource_params.keys.map(&:to_sym) & resource_class.authentication_keys)
18+
19+
conditions = []
20+
values = {}
21+
fields.each do |f|
22+
q = " #{f.to_s} = :#{f.to_s} "
23+
# fix for mysql default case insensitivity
24+
if ActiveRecord::Base.connection.adapter_name.downcase.starts_with? 'mysql'
25+
q = "BINARY " + q
26+
end
27+
conditions.push(q)
28+
values[f.to_sym] = get_case_insensitive_field_from_resource_params(f)
2029
end
2130

22-
@resource = resource_class.where(q, value).first
31+
conditions.push(' provider = :provider')
32+
values[:provider] = provider.to_s
33+
34+
@resource = resource_class.where([conditions.join(" AND "), values]).first
2335
end
2436

2537
def resource_class(m=nil)

app/controllers/devise_token_auth/passwords_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def create
2828
end
2929

3030
@email = get_case_insensitive_field_from_resource_params(:email)
31-
@resource = find_resource(:uid, @email)
31+
@resource = find_resource
3232

3333
@errors = nil
3434
@error_status = 400

app/controllers/devise_token_auth/sessions_controller.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,10 @@ def new
1010

1111
def create
1212
# Check
13-
field = (resource_params.keys.map(&:to_sym) & resource_class.authentication_keys).first
1413

15-
@resource = nil
16-
if field
17-
q_value = get_case_insensitive_field_from_resource_params(field)
14+
@resource = find_resource
1815

19-
@resource = find_resource(field, q_value)
20-
end
21-
22-
if @resource && valid_params?(field, q_value) && (!@resource.respond_to?(:active_for_authentication?) || @resource.active_for_authentication?)
16+
if @resource && (!@resource.respond_to?(:active_for_authentication?) || @resource.active_for_authentication?)
2317
valid_password = @resource.valid_password?(resource_params[:password])
2418
if (@resource.respond_to?(:valid_for_authentication?) && !@resource.valid_for_authentication? { valid_password }) || !valid_password
2519
render_create_error_bad_credentials

app/controllers/devise_token_auth/unlocks_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def create
1010
end
1111

1212
@email = get_case_insensitive_field_from_resource_params(:email)
13-
@resource = find_resource(:email, @email)
13+
@resource = find_resource
1414

1515
@errors = nil
1616
@error_status = 400

0 commit comments

Comments
 (0)