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

Rotate confirmation tokens after successful confirmation. #54

Merged
merged 1 commit into from
Jan 7, 2022
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class User < ApplicationRecord
validates :email, format: {with: URI::MailTo::EMAIL_REGEXP}, presence: true, uniqueness: true

def confirm!
regenerate_confirmation_token
update_columns(confirmed_at: Time.current)
end

Expand Down Expand Up @@ -160,6 +161,7 @@ end
> - The `has_secure_password` method is added to give us an [API](https://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html#method-i-has_secure_password) to work with the `password_digest` column.
> - The `has_secure_token :confirmation_token` method is added to give us an [API](https://api.rubyonrails.org/classes/ActiveRecord/SecureToken/ClassMethods.html#method-i-has_secure_token) to work with the `confirmation_token` column.
> - The `confirm!` method will be called when a user confirms their email address. We still need to build this feature.
> - Note that we call `regenerate_confirmation_token` to ensure their `confirmation_token` is reset so that it cannot be used again.
> - The `confirmed?` and `unconfirmed?` methods allow us to tell whether a user has confirmed their email address or not.
> - The `confirmation_token_is_valid?` method tells us if the confirmation token is expired or not. This can be controlled by changing the value of the `CONFIRMATION_TOKEN_EXPIRATION_IN_SECONDS` constant. This will be useful when we build the confirmation mailer.

Expand Down Expand Up @@ -845,6 +847,7 @@ class User < ApplicationRecord
if unconfirmed_email.present?
return false unless update(email: unconfirmed_email, unconfirmed_email: nil)
end
regenerate_confirmation_token
update_columns(confirmed_at: Time.current)
else
false
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def confirm!
if unconfirmed_email.present?
return false unless update(email: unconfirmed_email, unconfirmed_email: nil)
end
regenerate_confirmation_token
update_columns(confirmed_at: Time.current)
else
false
Expand Down
4 changes: 3 additions & 1 deletion test/controllers/confirmations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class ConfirmationsControllerTest < ActionDispatch::IntegrationTest
test "should confirm unconfirmed user" do
freeze_time

get edit_confirmation_path(@unconfirmed_user.confirmation_token)
assert_changes "@unconfirmed_user.reload.confirmation_token" do
get edit_confirmation_path(@unconfirmed_user.confirmation_token)
end

assert_equal Time.current, @unconfirmed_user.reload.confirmed_at
assert @unconfirmed_user.reload.confirmed?
Expand Down