-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to update their email address (#1745)
* Change email address * Email confirmation * Email change test * Lint * Schema reset * Set test email sender * Select specific user fixture * Refactor/cleanup * Remove unused email_confirmation_token * Current user would never be true * Fix translation test failures
- Loading branch information
Showing
28 changed files
with
225 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class EmailConfirmationsController < ApplicationController | ||
skip_before_action :set_request_details, only: :new | ||
skip_authentication only: :new | ||
|
||
def new | ||
# Returns nil if the token is invalid OR expired | ||
@user = User.find_by_token_for(:email_confirmation, params[:token]) | ||
|
||
if @user&.unconfirmed_email && @user&.update( | ||
email: @user.unconfirmed_email, | ||
unconfirmed_email: nil | ||
) | ||
redirect_to new_session_path, notice: t(".success_login") | ||
else | ||
redirect_to root_path, alert: t(".invalid_token") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module EmailConfirmationsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class EmailConfirmationMailer < ApplicationMailer | ||
# Subject can be set in your I18n file at config/locales/en.yml | ||
# with the following lookup: | ||
# | ||
# en.email_confirmation_mailer.confirmation_email.subject | ||
# | ||
def confirmation_email | ||
@user = params[:user] | ||
@subject = t(".subject") | ||
@cta = t(".cta") | ||
@confirmation_url = new_email_confirmation_url(token: @user.generate_token_for(:email_confirmation)) | ||
|
||
mail to: @user.unconfirmed_email, subject: @subject | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
app/views/email_confirmation_mailer/confirmation_email.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<h1><%= t(".greeting") %></h1> | ||
|
||
<p><%= t(".body") %></p> | ||
|
||
<%= link_to @cta, @confirmation_url, class: "button" %> | ||
|
||
<p class="footer"><%= t(".expiry_notice", hours: 24) %></p> |
9 changes: 9 additions & 0 deletions
9
app/views/email_confirmation_mailer/confirmation_email.text.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
EmailConfirmation#confirmation_email | ||
|
||
<%= t(".greeting") %> | ||
|
||
<%= t(".body") %> | ||
|
||
<%= t(".cta") %>: <%= @confirmation_url %> | ||
|
||
<%= t(".expiry_notice", hours: 24) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,14 @@ | |
config.eager_load = ENV["CI"].present? | ||
|
||
# Configure public file server for tests with Cache-Control for performance. | ||
config.public_file_server.enabled = true | ||
config.public_file_server.headers = { | ||
"Cache-Control" => "public, max-age=#{1.hour.to_i}" | ||
} | ||
|
||
# Set default sender email for tests | ||
ENV["EMAIL_SENDER"] = "[email protected]" | ||
|
||
# Show full error reports and disable caching. | ||
config.consider_all_requests_local = true | ||
config.action_controller.perform_caching = false | ||
|
@@ -69,4 +73,6 @@ | |
config.active_record.encryption.encrypt_fixtures = true | ||
|
||
config.autoload_paths += %w[test/support] | ||
|
||
config.action_mailer.default_url_options = { host: "example.com" } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
en: | ||
email_confirmation_mailer: | ||
confirmation_email: | ||
subject: "Maybe: Confirm your email change" | ||
greeting: "Hello!" | ||
body: "You recently requested to change your email address. Click the button below to confirm this change." | ||
cta: "Confirm email change" | ||
expiry_notice: "This link will expire in %{hours} hours." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class AddEmailConfirmationToUsers < ActiveRecord::Migration[7.2] | ||
def change | ||
add_column :users, :unconfirmed_email, :string | ||
add_column :users, :email_confirmation_token, :string | ||
add_column :users, :email_confirmation_sent_at, :datetime | ||
|
||
add_index :users, :email_confirmation_token, unique: true | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
db/migrate/20250130214500_remove_email_confirmation_sent_at_from_users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class RemoveEmailConfirmationSentAtFromUsers < ActiveRecord::Migration[7.2] | ||
def change | ||
remove_column :users, :email_confirmation_sent_at, :datetime | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
db/migrate/20250131171943_remove_email_confirmation_token_from_users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class RemoveEmailConfirmationTokenFromUsers < ActiveRecord::Migration[7.2] | ||
def change | ||
remove_index :users, :email_confirmation_token | ||
remove_column :users, :email_confirmation_token, :string | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require "test_helper" | ||
|
||
class EmailConfirmationsControllerTest < ActionDispatch::IntegrationTest | ||
test "should get confirm" do | ||
user = users(:new_email) | ||
user.update!(unconfirmed_email: "[email protected]") | ||
token = user.generate_token_for(:email_confirmation) | ||
|
||
get new_email_confirmation_path(token: token) | ||
assert_redirected_to new_session_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,13 @@ family_member: | |
last_name: Dylan | ||
email: [email protected] | ||
password_digest: <%= BCrypt::Password.create('password') %> | ||
onboarded_at: <%= 3.days.ago %> | ||
onboarded_at: <%= 3.days.ago %> | ||
|
||
new_email: | ||
family: empty | ||
first_name: Test | ||
last_name: User | ||
email: [email protected] | ||
unconfirmed_email: [email protected] | ||
password_digest: <%= BCrypt::Password.create('password123') %> | ||
onboarded_at: <%= Time.current %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require "test_helper" | ||
|
||
class EmailConfirmationMailerTest < ActionMailer::TestCase | ||
test "confirmation_email" do | ||
user = users(:new_email) | ||
user.unconfirmed_email = "[email protected]" | ||
|
||
mail = EmailConfirmationMailer.with(user: user).confirmation_email | ||
assert_equal I18n.t("email_confirmation_mailer.confirmation_email.subject"), mail.subject | ||
assert_equal [ user.unconfirmed_email ], mail.to | ||
assert_equal [ "[email protected]" ], mail.from | ||
assert_match "confirm", mail.body.encoded | ||
end | ||
end |
Oops, something went wrong.