1
votes

I am trying to regenerate confirmation token for the users who have not confirmed their accounts yet. I want to resend confirmation email to the user say after few days.I am following this link to regenerate the new token for that user, and its generating it but still its not working.

https://github.com/plataformatec/devise/issues/2615.

and I am following these steps on my console locally.

@user = User.find_by_email("[email protected]")
token = Devise.token_generator.generate(@user.class, :confirmation_token)

then in token i am getting array such as 

token = ["gfbgk4535843tbk","8545kjbng8hguhggre8gergerkgjebg8gergkerjgg9ergejgn"]

then i am just sending an email like this

Devise::Mailer.confirmation_instructinos(@user,token.last).deliver

then its delivering an email to that user, but when user clicks on confirm account it takes user to the site but when user tries to login its not working.

1
its not working for rails 4Hemant Patil
have u checked config.reconfirmable = true by doing this in config/devise.rb file ???Gaurav Mamidwar
According to devise.rb file i simply did config.reconfirmable = true and added unconfirmed_email column to the users table. it did not helpHemant Patil

1 Answers

8
votes

You can resend confirmation email using Devise built in methods:

users = User.where('confirmation_token IS NOT NULL')
users.each do |user|
  user.send_confirmation_instructions
end

send_confirmation_instructions method should generate a new token if token period expired. I've been using this approach a few times and it did what was expected.

Also you could try resend_confirmation_instructions,

  # Resend confirmation token.
  # Regenerates the token if the period is expired.
  def resend_confirmation_instructions
    pending_any_confirmation do
      send_confirmation_instructions
    end
  end

comments above the method clearly states that token will be regenerated. I haven't tried this one yet.