def confirm_email
if request.post?
- token = UserToken.find_by(:token => params[:confirm_string])
- if token&.user&.new_email?
- self.current_user = token.user
+ token = params[:confirm_string]
+
+ self.current_user = User.find_by_token_for(:new_email, token) ||
+ UserToken.unexpired.find_by(:token => params[:confirm_string])&.user
+
+ if current_user&.new_email?
current_user.email = current_user.new_email
current_user.new_email = nil
current_user.email_valid = true
current_user.tokens.delete_all
session[:user] = current_user.id
session[:fingerprint] = current_user.fingerprint
- elsif token
+ elsif current_user
flash[:error] = t ".failure"
else
flash[:error] = t ".unknown_token"
with_recipient_locale user do
@address = user.new_email
@url = url_for(:controller => "confirmations", :action => "confirm_email",
- :confirm_string => token.token)
+ :confirm_string => token)
mail :to => user.new_email,
:subject => t(".subject")
def test_confirm_email_get
user = create(:user)
- confirm_string = user.tokens.create.token
+ confirm_string = user.generate_token_for(:new_email)
get user_confirm_email_path, :params => { :confirm_string => confirm_string }
assert_response :success
def test_confirm_email_success
user = create(:user, :new_email => "test-new@example.com")
stub_gravatar_request(user.new_email)
- confirm_string = user.tokens.create.token
+ confirm_string = user.generate_token_for(:new_email)
post user_confirm_email_path, :params => { :confirm_string => confirm_string }
assert_response :redirect
def test_confirm_email_already_confirmed
user = create(:user)
- confirm_string = user.tokens.create.token
+ confirm_string = user.generate_token_for(:new_email)
post user_confirm_email_path, :params => { :confirm_string => confirm_string }
assert_response :redirect
# switch to email that has a gravatar
user = create(:user, :new_email => "test-new@example.com")
stub_gravatar_request(user.new_email, 200)
- confirm_string = user.tokens.create.token
+ confirm_string = user.generate_token_for(:new_email)
# precondition gravatar should be turned off
assert_not user.image_use_gravatar
post user_confirm_email_path, :params => { :confirm_string => confirm_string }
# switch to email without a gravatar
user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
stub_gravatar_request(user.new_email, 404)
- confirm_string = user.tokens.create.token
+ confirm_string = user.generate_token_for(:new_email)
# precondition gravatar should be turned on
assert user.image_use_gravatar
post user_confirm_email_path, :params => { :confirm_string => confirm_string }