Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update process for user to change email #104

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@

<%% if Current.user.verified? %>
<h1>Change your email</h1>
<%% else %>
<h1>Verify your email</h1>
<p>We sent a verification email to the address below. Check that email and follow those instructions to confirm it's your email address.</p>
<p><%%= button_to "Re-send verification email", identity_email_verification_path %></p>
<%% end %>
<%%= form_with(url: identity_email_path, method: :patch) do |form| %>
<%% if @user.errors.any? %>
<div style="color: red">
<h2><%%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<%% @user.errors.each do |error| %>
<li><%%= error.full_message %></li>
<%% end %>
</ul>
</div>
<%% end %>

<div>
<%%= form.label :email, "New email", style: "display: block" %>
<%%= form.email_field :email, required: true, autofocus: true %>
</div>

<%%= form_with(url: identity_email_path, method: :patch) do |form| %>
<%% if @user.errors.any? %>
<div style="color: red">
<h2><%%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<div>
<%%= form.label :password_challenge, style: "display: block" %>
<%%= form.password_field :password_challenge, required: true, autocomplete: "current-password" %>
</div>

<ul>
<%% @user.errors.each do |error| %>
<li><%%= error.full_message %></li>
<%% end %>
</ul>
<div>
<%%= form.submit "Save changes" %>
</div>
<%% end %>

<div>
<%%= form.label :email, "New email", style: "display: block" %>
<%%= form.email_field :email, required: true, autofocus: true %>
</div>

<div>
<%%= form.label :password_challenge, style: "display: block" %>
<%%= form.password_field :password_challenge, required: true, autocomplete: "current-password" %>
</div>

<div>
<%%= form.submit "Save changes" %>
</div>
<%% else %>
<h1>Verify your email</h1>
<p>You must verify your current email address before it can be changed in your profile.</p>
<p>We sent a verification email to the address below. Check that email and follow those instructions to confirm it's your email address.</p>
<p><%%= @user.email %></p>
<p><%%= button_to "Re-send verification email", identity_email_verification_path %></p>
<%% end %>

<br>
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/authentication/templates/models/user.rb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class User < ApplicationRecord

normalizes :email, with: -> { _1.strip.downcase }

before_validation if: :email_changed?, on: :update do
before_update if: :email_changed? do
self.verified = false
end
<%- if two_factor? %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ class Identity::EmailsTest < ApplicationSystemTestCase
assert_text "Your email has been changed"
end

test "updating the email with wrong password" do
click_on "Change email address"

fill_in "New email", with: "[email protected]"
fill_in "Password challenge", with: "Wrong1*3*5*"
click_on "Save changes"

assert_text "Password challenge is invalid"
end

test "sending a verification email" do
@user.update! verified: false

Expand Down