Skip to content

Commit

Permalink
fix: .password_valid? for nil
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellberg committed Apr 3, 2024
1 parent d611832 commit c9e7047
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/controllers/users/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def show_otp_code_form
session.delete(:otp_user_id)

self.resource = User.find_by(email: sign_in_params[:email])
if resource.valid_password?(sign_in_params[:password]) && resource.otp_required_for_login?
if resource&.otp_required_for_login? && resource&.valid_password?(sign_in_params[:password])
session[:otp_user_id] = resource.id
render :otp, status: :unprocessable_entity
end
Expand Down
13 changes: 13 additions & 0 deletions test/controllers/users/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,17 @@ class Users::SessionsControllerTest < ActionDispatch::IntegrationTest
assert_response :redirect
assert_redirected_to dashboard_path
end

test "renders form again if invalid email" do
post user_session_path, params: {user: {email: "unknown.email", password: "randompassword"}}
assert_response :unprocessable_entity
assert_template "users/sessions/new"
end

test "renders form again if invalid password" do
user = create(:user)
post user_session_path, params: {user: {email: user.email, password: "randompassword"}}
assert_response :unprocessable_entity
assert_template "users/sessions/new"
end
end

0 comments on commit c9e7047

Please sign in to comment.