Skip to content

Add Authentication to Rails with Devise

Use this when adding Devise authentication to an existing Rails app.

Add Devise to the Gemfile.

gem "devise"

Install the gem.

Terminal window
bundle install

Run the Devise installer.

Terminal window
bin/rails generate devise:install

In development, Devise needs a host for mailer URLs. Add this inside the development environment config.

config.action_mailer.default_url_options = { host: "localhost", port: 3000 }

Generate the Devise user model and run the migration.

Terminal window
bin/rails generate devise User
bin/rails db:migrate

Devise modules are enabled in the model file. For a generated User model, edit app/models/user.rb.

class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end

Common modules:

ModuleWhat it doesMain file to edit
database_authenticatableStores an encrypted password and authenticates users from the database.app/models/user.rb
registerableLets users sign up, edit, and delete their account.app/models/user.rb
recoverableAdds password reset emails and reset tokens.app/models/user.rb, Devise migration
rememberableLets users stay signed in with a remember-me cookie.app/models/user.rb, Devise migration
validatableAdds default email and password validations.app/models/user.rb
confirmableRequires users to confirm their email before signing in.app/models/user.rb, Devise migration
lockableLocks an account after too many failed sign-in attempts.app/models/user.rb, Devise migration, config/initializers/devise.rb
timeoutableSigns users out after a period of inactivity.app/models/user.rb, config/initializers/devise.rb
trackableTracks sign-in count, timestamps, and IP addresses.app/models/user.rb, Devise migration
omniauthableAdds OmniAuth provider support, such as GitHub or Google login.app/models/user.rb, config/initializers/devise.rb

To enable a module, add it to the devise line.

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:confirmable

Some modules need database columns. If the module is commented out in the generated Devise migration, uncomment that section before running bin/rails db:migrate.

## Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email

Some modules also have settings in config/initializers/devise.rb.

config.timeout_in = 30.minutes
config.maximum_attempts = 5

Use Devise route helpers for sign in, sign out, and registration links.

<% if user_signed_in? %>
<%= button_to "Sign out", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "Sign in", new_user_session_path %>
<%= link_to "Sign up", new_user_registration_path %>
<% end %>

Require a signed-in user before accessing a controller action.

before_action :authenticate_user!

Confirm the Devise routes are available.

Terminal window
bin/rails routes | grep user

Commit the Devise setup after the migration and views are working.

Terminal window
git add .
git commit -m "Add Devise authentication"