Skip to content

Commit 28a06ce

Browse files
committed
Support authentication with multiple fields
1 parent b86b217 commit 28a06ce

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

app/controllers/devise_token_auth/concerns/resource_finder.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@ def get_case_insensitive_field_from_resource_params(field)
1313
end
1414

1515
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
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: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,8 @@ 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)
18-
19-
@resource = find_resource(field, q_value)
20-
end
14+
@resource = find_resource
2115

2216
if @resource && valid_params?(field, q_value) && (!@resource.respond_to?(:active_for_authentication?) || @resource.active_for_authentication?)
2317
valid_password = @resource.valid_password?(resource_params[:password])

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)