Fix bugs in Settings page when changes to the model are incomplete

Ahh okay tricky lil thing: if you show the settings page with a partial
change to `AuthUser` that didn't get saved, it can throw off the state
of some stuff. For example, if you don't have a password yet, then
enter a new password but leave the confirmation box blank, then you'll
correctly see "Password confirmation can't be blank", but you'll *also*
then be prompted for your "Current password", even though you don't
have one yet, because `@auth_user.uses_password?` is true now.

In this change, we extend the Settings form to use two copies of the
`AuthUser`. One is the copy with changes on it, and the other is the
"persisted" copy, which we check for parts of the UI that care about
what's actually saved, vs form state.
This commit is contained in:
Emi Matchu 2024-04-09 06:23:54 -07:00
parent f450937952
commit 95c1a4f391
2 changed files with 24 additions and 11 deletions

View file

@ -14,7 +14,9 @@ class AuthUsersController < ApplicationController
end end
def edit def edit
@auth_user = current_auth_user # For the edit form, the auth user *is* the persisted auth user.
@persisted_auth_user = current_auth_user
@auth_user = @persisted_auth_user
end end
def new def new
@ -22,7 +24,16 @@ class AuthUsersController < ApplicationController
end end
def update def update
# When updating, we hold onto the original `@persisted_auth_user`, then
# make our changes to `@auth_user`. That way, the form can check the *live*
# value of `uses_password?` to decide whether to show the "Current
# password" field, instead of getting thrown off if the password changed
# but the record didn't get saved.
#
# HACK: Is there a way to get the kind of copy we want for real? `dup`
# actually returns a *new* unsaved record with the same attributes.
@auth_user = load_auth_user @auth_user = load_auth_user
@persisted_auth_user = @auth_user.dup
if @auth_user.update_with_password(auth_user_params) if @auth_user.update_with_password(auth_user_params)
# NOTE: Changing the password will sign you out, so make sure we stay # NOTE: Changing the password will sign you out, so make sure we stay

View file

@ -39,7 +39,7 @@
</fieldset> </fieldset>
<%# Current password is only required if you have one! %> <%# Current password is only required if you have one! %>
<% if @auth_user.uses_password? %> <% if @persisted_auth_user.uses_password? %>
<fieldset> <fieldset>
<div class="field"> <div class="field">
<%= f.label :current_password %> <%= f.label :current_password %>
@ -55,13 +55,14 @@
</div> </div>
<% end %> <% end %>
<% if @auth_user.uses_neopass? %> <% if @persisted_auth_user.uses_neopass? %>
<%= form_with url: user_neopass_connection_path(@auth_user.user), <%= form_with url: user_neopass_connection_path(@auth_user.user),
method: :delete, class: "settings-form", data: { method: :delete, class: "settings-form", data: {
turbo_confirm: "Are you sure? Without a NeoPass, you'll need to use " + turbo_confirm: "Are you sure? Without a NeoPass, you'll need to use " +
"your password or your recovery email \"#{@auth_user.email}\" to " + "your password or your recovery email " +
"log in again.\n\nMake sure you have everything all set up first! " + "\"#{@persisted_auth_user.email}\" to log in again.\n\nMake sure " +
"Otherwise, you might be locked out of this account forever!" "you have everything all set up first! Otherwise, you might be " +
"locked out of this account forever!"
} do |form| } do |form|
%> %>
<h2>Your NeoPass</h2> <h2>Your NeoPass</h2>
@ -69,7 +70,7 @@
<strong> <strong>
NeoPass ID: NeoPass ID:
</strong> </strong>
<%= @auth_user.neopass_friendly_id %> <%= @persisted_auth_user.neopass_friendly_id %>
</section> </section>
<section class="neopass-explanation"> <section class="neopass-explanation">
<p> <p>
@ -78,23 +79,24 @@
you can still use "Forgot your password?" to recover your Dress to you can still use "Forgot your password?" to recover your Dress to
Impress account, using the Email saved in "Your info". Impress account, using the Email saved in "Your info".
</p> </p>
<% if !@auth_user.uses_password? && !@auth_user.email %> <% if !@persisted_auth_user.uses_password? && !@persisted_auth_user.email? %>
<p> <p>
You can't remove this NeoPass yet, because you need to either set a You can't remove this NeoPass yet, because you need to either set a
password or a recovery email first. (Ideally both!) password or a recovery email first. (Ideally both!)
</p> </p>
<% elsif !@auth_user.uses_password? %> <% elsif !@persisted_auth_user.uses_password? %>
<p> <p>
Be extra careful here! Your account doesn't have a password set. Be extra careful here! Your account doesn't have a password set.
</p> </p>
<% elsif !@auth_user.email? %> <% elsif !@persisted_auth_user.email? %>
<p> <p>
Be extra careful here! Your account doesn't have an email set. Be extra careful here! Your account doesn't have an email set.
</p> </p>
<% end %> <% end %>
</section> </section>
<%= form.submit "Disconnect your NeoPass", <%= form.submit "Disconnect your NeoPass",
disabled: !@auth_user.uses_password? && !@auth_user.email? %> disabled: !@persisted_auth_user.uses_password? &&
!@persisted_auth_user.email? %>
<% end %> <% end %>
<% elsif can_use_neopass %> <% elsif can_use_neopass %>
<%= form_with url: auth_user_neopass_omniauth_authorize_path(intent: "connect"), <%= form_with url: auth_user_neopass_omniauth_authorize_path(intent: "connect"),