Skip to content

How To: Add :confirmable to Users

Maud de Vries edited this page Jan 20, 2016 · 35 revisions
Clone this wiki locally

If you find yourself needing to introduce confirmable to your User model (stored as the users table) after the application has already been used for sometime, you will end up marking existing users as unconfirmed in the application.

For example, assume that you have a system full of users and you need to implement email notifications for user accounts. In doing this, existing user accounts will be left unconfirmed thus unable to log in.

Here's how you can introduce confirmable to users while also marking existing users as confirmed.

First, add devise :confirmable to your models/user.rb file

devise :registerable, :confirmable

Then, do the migration as:

rails g migration add_confirmable_to_devise

Will generate db/migrate/YYYYMMDDxxx_add_confirmable_to_devise.rb. Add the following to it in order to do the migration.

class AddConfirmableToDevise < ActiveRecord::Migration
  # Note: You can't use change, as User.update_all will fail in the down migration
  def up
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :datetime
    add_column :users, :confirmation_sent_at, :datetime
    # add_column :users, :unconfirmed_email, :string # Only if using reconfirmable
    add_index :users, :confirmation_token, unique: true
    # User.reset_column_information # Need for some types of updates, but not for update_all.
    # To avoid a short time window between running the migration and updating all existing
    # users as confirmed, do the following
    execute("UPDATE users SET confirmed_at = NOW()")
    # All existing user accounts should be able to log in after this.
    # Remind: Rails using SQLite as default. And SQLite has no such function :NOW.
    # Use :date('now') instead of :NOW when using SQLite.
    # => execute("UPDATE users SET confirmed_at = date('now')")
    # Or => User.all.update_all confirmed_at: Time.now
  end

  def down
    remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
    # remove_columns :users, :unconfirmed_email # Only if using reconfirmable
  end
end

You can also Generate views if haven't already

rails generate devise:views users

Do the migration rake db:migrate

Restart the server.

If you are not using :reconfirmable (i.e leave the commented out lines as they are in the change method described above), update the configuration in config/initializers/devise.rb

config.reconfirmable = false

Before you can actually send the confirmation mail, you need the Devise::Mailer or a custom mailer configured.

Redirecting user

If you want to redirect the user to a specific url after they clicked the link in the confirmation email, override the after_inactive_sign_up_path in your registrations_controller:

(*** I think this part is wrong? The overwritten method should be after_confirmation_path_for in Devise::ConfirmationsController. The authors please confirm. ***)

class Users::RegistrationsController < Devise::RegistrationsController
  def after_inactive_sign_up_path_for(resource_or_scope)
    session["user_return_to"] || root_path
  end
end

Allowing Unconfirmed Access

If you want to add a "grace period" where unconfirmed users may still login, use the allow_unconfirmed_access_for config option (which defaults to 0):

# in Devise Initializer
config.allow_unconfirmed_access_for = 365.days

Alternatively, you may want to skip required confirmation all-together:

# in User.rb
protected
def confirmation_required?
  false
end
Something went wrong with that request. Please try again.