Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually unconfirm a user in devise, rails

I'm trying to do something in my app, where if a user hasn't logged in within 90 days, their account will be set to unconfirmed, and they'll be emailed to re-confirm their email address.

I know I can resend the email with this:

Devise::Mailer.confirmation_instructions(@user).deliver

I'm seeing these related fields in the schema -

 t.string   "unconfirmed_email"
 t.string   "confirmation_token"
 t.datetime "confirmed_at"
 t.datetime "confirmation_sent_at"

Will setting them all to nil, and the unconfirmed email back to the regular email make this work?

like image 920
Elliot Avatar asked Jul 31 '13 21:07

Elliot


1 Answers

Based on devise code, clearing out confirmation_token and setting the unconfirmed_email to original email should resend the confirmation. Since you have unconfirmed_email column, i am assuming that you have the reconfirmable enabled in the devise config.

You can see the related code from the confirmable model below

  after_update  :send_reconfirmation_instructions,  :if => :reconfirmation_required? 


  def send_reconfirmation_instructions
    @reconfirmation_required = false

    unless @skip_confirmation_notification
      send_confirmation_instructions
    end
  end

 # Send confirmation instructions by email
  def send_confirmation_instructions
    ensure_confirmation_token!

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

 # Generate a confirmation token unless already exists and save the record.
  def ensure_confirmation_token!
    generate_confirmation_token! if should_generate_confirmation_token?
  end 

  def should_generate_confirmation_token?
      confirmation_token.nil? || confirmation_period_expired?
    end
like image 177
usha Avatar answered Sep 24 '22 11:09

usha