Thank you! Your feedback has been delivered
Thank you! Your feedback has been sent

Devise: When changing a user

When changing the email address of a user, devise for some reason sends the confirmation email to the old email address instead of the new one.

How can change this behavior so Devise will send the email to the actual email address that needs confirmation?

This is my original unanswered question on Stack Overflow - http://stackoverflow.com/questions/21269598/devise-when-changing-a-users-email-devise-sends-a-confirmation-email-to-old-e?noredirect=1#comment32047451_21269598

User Gravatar

dbenjamin57

Posted Jan 22 2014 1:36 UTC

$50


  • Assigned To ariejan
  • Solved
  • ruby
    rails
    ruby-on-rails
  • 1808 Views

4 Replies


Things to consider:

  • Do you have :reconfirmable enabled?
  • Does your User table have a column unconfirmed_email? If not, add it.
  • Did you override any Devise methods that may interfere with confirmation emails?

If these are not the answer, please add your User model, any controller code and db migrations to further investigate the issue.

User Gravatar

ariejan

Posted Jan 22 2014 1:46 UTC

Solution

This didn't solve your task? Get your own custom solution.

Without seeing your code it is difficult to see why this behaves like this

in devise confirmable docs its says

+reconfirmable+: requires any email changes to be confirmed (exactly the same way as initial account confirmation) to be applied. Requires additional unconfirmed_email db field to be setup (t.reconfirmable in migrations). Until confirmed new email is stored in unconfirmed email column, and copied to email column on successful confirmation.

so notice this code from devise confirmable

 def postpone_email_change_until_confirmation_and_regenerate_confirmation_token
          @reconfirmation_required = true
          self.unconfirmed_email = self.email
          self.email = self.email_was
          generate_confirmation_token
        end

 def send_confirmation_instructions
        unless @raw_confirmation_token
          generate_confirmation_token!
        end

        opts = pending_reconfirmation? ? { :to => unconfirmed_email } : { }
        send_devise_notification(:confirmation_instructions, @raw_confirmation_token, opts)
      end

so the real email doesn't change until it is confirmed, and the email is sent to unconfirmed_email

so see if you have that column declared and set up if that still doesn't work, you can override the above function to force the email to be sent to the unconfirmed email, but that could be a problem since if you change the email before it is confirmed then the the user could be locked out, hence the uncofirmed_email

User Gravatar

guyisra

Posted Jan 22 2014 1:58 UTC

The user table has the uncofirmed_email. Actually, this is what gets updated when I change the user email via update_attributes. The only problem is that once I use update attributes it automatically sends a confirmation email to the old email address.

Will check if I overriden a method I shouldnt have and keep you guys posted.

User Gravatar

dbenjamin57

Posted Jan 22 2014 2:01 UTC

is your devise code "stock" code or have you changed anything?

User Gravatar

guyisra

Posted Jan 22 2014 2:05 UTC

Add a reply

By posting a reply on CodersClan you agree to our Terms & Conditions