Skip to content

Commit 2ba1ef7

Browse files
Refactor SessionsController
1 parent b4e789a commit 2ba1ef7

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

app/controllers/devise_token_auth/sessions_controller.rb

+10-11
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ def new
1111
end
1212

1313
def create
14-
# Check
15-
field = (resource_params.keys.map(&:to_sym) & resource_class.authentication_keys).first
16-
17-
@resource = nil
18-
if field
14+
if field = (resource_params.keys.map(&:to_sym) & resource_class.authentication_keys).first
1915
q_value = get_case_insensitive_field_from_resource_params(field)
2016

2117
@resource = find_resource(field, q_value)
@@ -41,12 +37,7 @@ def create
4137
render_create_error_not_confirmed
4238
end
4339
else
44-
# In order to avoid timing attacks in paranoid mode, we want the password hash to be
45-
# calculated even if no resource has been found. Devise's DatabaseAuthenticatable warden
46-
# strategy handles this case similarly:
47-
# https://github.com/heartcombo/devise/blob/main/lib/devise/strategies/database_authenticatable.rb
48-
resource_class.new.password = resource_params[:password] if Devise.paranoid
49-
40+
hash_password_in_paranoid_mode
5041
render_create_error_bad_credentials
5142
end
5243
end
@@ -150,5 +141,13 @@ def create_and_assign_token
150141
@resource.save!
151142
end
152143
end
144+
145+
def hash_password_in_paranoid_mode
146+
# In order to avoid timing attacks in paranoid mode, we want the password hash to be
147+
# calculated even if no resource has been found. Devise's DatabaseAuthenticatable warden
148+
# strategy handles this case similarly:
149+
# https://github.com/heartcombo/devise/blob/main/lib/devise/strategies/database_authenticatable.rb
150+
resource_class.new.password = resource_params[:password] if Devise.paranoid
151+
end
153152
end
154153
end

0 commit comments

Comments
 (0)