Add Email Confirmations to Your Rails App

email-icon.jpg

Although this seems pretty easy, this is something that I have struggled with. In turn, I wanted to write a post so that if there is someone else out there struggling as well, they have some steps to help them out.

So you want to add email confirmations to your app? Great. Now how do you do that.

In this case, I am using devise. So these steps are for those using the devise gem in their rails app.

1. Update your users model by adding confirmable
When you set up devise, you probably made a user model. Go to that model and update the code by adding confirmable

devise :registerable, :confirmable

2. Generate a migration
Make sure that you generate a migration to add confirmable to your users table.

You can create a migration with code like this:

rails g migration add_confirmable_to_devise

In the migration file, you will need to add this, making selections for whether or not you will do reconfirmable. This is relevant if you are introducing confirmations after you already have users in your database.

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.
  end

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

Then run:

rake db:migrate

3. If you are using reconfirmable…
then you will need to ensure that this code in config/initializers/devise.rb is set to:

config.reconfirmable = true

This code should already exist, as set to false. You may have to un-comment so that it is run.

There is also this code in the initializer, which you can set to give a grace period for confirming

config.allow_unconfirmed_access_for = 5.days

4. Generate Devise Views
If you have not generated devise views yet, you should do so. You can do that by typing this in the terminal. Either one.

rails generate devise:views # global
rails generate devise:views users # scoped

5. Config Environments
You will need to ensure that both your development and production environments are configured properly. In config/environments/development you should have this code

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {:address => "localhost", :port => 1025}

and you should already have this code from setting up devise

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

In config/environments/production you may need something like this.

config.action_mailer.default_url_options = {:host => 'yourdomain.com'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address => "127.0.0.1",
  :port    => 25,
  :domain  => 'yourdomain.com'
}

6. Make sure you got your mailers working
This may not be a requirement, but it is a good idea. If you need help with this, check out my previous post on setting up action mailers in rails 4.

 
95
Kudos
 
95
Kudos

Now read this

Back 2 The Start…

These last few years have been a constant whirlwind. From leaving my job, to learning how to code, to becoming a freelancer, and moving beyond, I have been constantly learning. And while I have learned to sustain drinking from the fire... Continue →