From f81a88d616ef2c794f333aea9058ee85135c503d Mon Sep 17 00:00:00 2001 From: Chris Lowis Date: Tue, 29 Oct 2024 12:27:21 +0000 Subject: [PATCH] Inline email update form into /account Note there are two fields on this page labelled "Current password" so we have to scope the Capybara selector in the system test more specifically to avoid errors. --- .../email_verifications_controller.rb | 2 +- app/controllers/identity/emails_controller.rb | 17 ++++----- app/views/identity/emails/edit.html.erb | 21 ---------- app/views/users/show.html.erb | 22 ++++++++++- config/routes.rb | 2 +- .../email_verifications_controller_test.rb | 2 +- .../identity/emails_controller_test.rb | 11 ++---- test/controllers/users_controller_test.rb | 5 --- test/system/identity/emails_test.rb | 38 ++++++++++++------- test/system/passwords_test.rb | 26 +++++++++---- 10 files changed, 76 insertions(+), 70 deletions(-) delete mode 100644 app/views/identity/emails/edit.html.erb diff --git a/app/controllers/identity/email_verifications_controller.rb b/app/controllers/identity/email_verifications_controller.rb index ff7520eb..8805b46a 100644 --- a/app/controllers/identity/email_verifications_controller.rb +++ b/app/controllers/identity/email_verifications_controller.rb @@ -26,7 +26,7 @@ def set_user token = EmailVerificationToken.find_signed!(params[:sid]) @user = token.user rescue StandardError - redirect_to edit_identity_email_path, alert: 'That email verification link is invalid' + redirect_to root_path, alert: 'That email verification link is invalid' end def send_email_verification diff --git a/app/controllers/identity/emails_controller.rb b/app/controllers/identity/emails_controller.rb index b317dd26..59a2c642 100644 --- a/app/controllers/identity/emails_controller.rb +++ b/app/controllers/identity/emails_controller.rb @@ -4,19 +4,16 @@ module Identity class EmailsController < ApplicationController before_action :set_user - def edit - authorize @user - end - def update authorize @user if !@user.authenticate(params[:current_password]) - redirect_to edit_identity_email_path, alert: 'The password you entered is incorrect' + flash[:emails_update_password_incorrect] = 'The password you entered is incorrect' + redirect_to account_path elsif @user.update(email: params[:email]) - redirect_to_root + resend_verification_email_and_redirect else - render :edit, status: :unprocessable_entity + render 'users/show', status: :unprocessable_entity end end @@ -26,12 +23,12 @@ def set_user @user = Current.user end - def redirect_to_root + def resend_verification_email_and_redirect if @user.email_previously_changed? resend_email_verification - redirect_to root_path, notice: 'Your email has been changed' + redirect_to account_path, notice: 'Your email has been changed' else - redirect_to root_path + redirect_to account_path end end diff --git a/app/views/identity/emails/edit.html.erb b/app/views/identity/emails/edit.html.erb deleted file mode 100644 index 46fe3926..00000000 --- a/app/views/identity/emails/edit.html.erb +++ /dev/null @@ -1,21 +0,0 @@ -<% if Current.user.verified? %> - <%= render 'shared/page_header', text: 'Change your email' %> -<% else %> - <%= render 'shared/page_header', text: 'Verify your email' %> - -
-

We sent a verification email to the address below. Check that email and follow those instructions to confirm it's your email address.

-

<%= button_to "Re-send verification email", identity_email_verification_path, class: 'py-3 px-5 bg-amber-600 hover:bg-amber-500 text-white font-medium cursor-pointer my-3' %>

-
-<% end %> - -<%= form_with(url: identity_email_path, method: :patch, builder: TailwindFormBuilder) do |form| %> - <%= render ModelErrorComponent.new(model: @user) %> - <%= form.email_field :email, label: { text: "New email" }, required: true, autofocus: true, class: 'w-full mb-3' %> - <%= form.password_field :current_password, required: true, autocomplete: "current-password", class: 'w-full mb-3' %> - <%= form.submit "Save changes", class: 'w-full mt-3' %> -<% end %> - -
- <%= text_link_to "Back", root_path %> -
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index a03946b9..4ebb5a17 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -27,7 +27,26 @@

Email address

-

form goes here

+ <% unless Current.user.verified? %> +
+

We sent a verification email to the address below. Check that email and follow those instructions to confirm it's your email address.

+

<%= button_to "Re-send verification email", identity_email_verification_path, class: 'py-3 px-5 bg-amber-600 hover:bg-amber-500 text-white font-medium cursor-pointer my-3' %>

+
+ <% end %> + + <%= form_with(url: identity_email_path, method: :patch, builder: TailwindFormBuilder) do |form| %> + <% if flash[:emails_update_password_incorrect] %> + <%= render ErrorBoxComponent.new(title: 'Incorrect password').with_content(flash[:emails_update_password_incorrect]) %> + <% end %> + <% if @user.errors.include?(:email) %> + <%= render ModelErrorComponent.new(model: @user) %> + <% end %> + <%= form.email_field :email, label: { text: "New email" }, required: true, autofocus: true, class: 'w-full mb-3' %> + <%= form.password_field :current_password, required: true, autocomplete: "current-password", class: 'w-full mb-3' %> +
+ <%= form.submit "Save changes", class: 'mt-3' %> +
+ <% end %>
@@ -41,7 +60,6 @@