Skip to content

Commit

Permalink
Display the 'password incorrect' message in an ErrorBoxComponent
Browse files Browse the repository at this point in the history
This matches the style of the ModelErrorComponent shown if the User
model itself has validation errors. I've added a specific flash key so
that this error box is not shown in the case where the `alert` key is
set for other reasons.
  • Loading branch information
chrislo committed Oct 28, 2024
1 parent 678c578 commit c519dd6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/controllers/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def update
authorize @user

if !@user.authenticate(params[:current_password])
redirect_to account_path, alert: 'The current password you entered is incorrect'
flash[:incorrect_password] = 'The current password you entered is incorrect'
redirect_to account_path
elsif @user.update(user_params)
redirect_to account_path, notice: 'Your password has been changed'
else
Expand Down
3 changes: 3 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
</div>
<div class="grow">
<%= form_with(url: password_path, method: :patch, builder: TailwindFormBuilder) do |form| %>
<% if flash[:incorrect_password] %>
<%= render ErrorBoxComponent.new(title: 'Incorrect password').with_content(flash[:incorrect_password]) %>
<% end %>
<%= render ModelErrorComponent.new(model: @user) %>
<%= form.password_field :current_password, required: true, autofocus: true, autocomplete: "current-password", class: "w-full mb-3" %>
<%= form.password_field :password, label: { text: "New password"}, required: true, autocomplete: "new-password", class: "w-full mb-3" %>
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/passwords_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class PasswordsControllerTest < ActionDispatch::IntegrationTest
password_confirmation: 'Secret6*4*2*' }

assert_redirected_to account_path
assert_equal 'The current password you entered is incorrect', flash[:alert]
assert_equal 'The current password you entered is incorrect', flash[:incorrect_password]
end
end

0 comments on commit c519dd6

Please sign in to comment.