Chapter 11: Authentication with Devise

Chapter 11: Authentication with Devise

AI Reading

Quick summary of this article

This chapter explains how to add user authentication to a Rails blog using the Devise gem. It covers installing the gem, generating a User model, setting up authentication routes, and protecting controller actions so only signed-in users can create, edit, or delete posts. Devise handles secure password storage, sessions, and reset tokens automatically.

  • Add the Devise gem to your Gemfile, run bundle install, then use rails generate devise:install to set it up.
  • Generate a User model with rails generate devise User and run rails db:migrate to create the necessary database columns like email and encrypted_password.
  • A single line in the routes file, devise_for :users, provides all authentication routes, including sign-up, sign-in, and sign-out pages.
  • Protect controller actions by adding before_action :authenticate_user!, which redirects visitors to the login page unless they are signed in.
  • Use the current_user helper and user_signed_in? method in views to display the signed-in user's email and show or hide sign-in/sign-out links.

Chapter 11: Authentication with Devise

So far anyone can create posts and comments on our blog. In a real application you usually want users to sign up, log in, and log out, so that only signed-in users can post. Rails does not include authentication out of the box, but the most popular solution is a gem called Devise.

In this chapter you will add Devise to the blog, create a User model, and protect your actions so only signed-in users can create posts.

🔩 Adding the Devise Gem

Open your Gemfile and add Devise:

# Gemfile
gem "devise"

Install the gem and run the Devise setup generator:

bundle install
rails generate devise:install

💾 Creating the User Model

Devise has a generator that creates a User model with everything needed for authentication:

rails generate devise User
rails db:migrate

This creates a User model with secure password handling and a migration that adds columns like email, encrypted_password, and remember/reset tokens.

🔗 Devise Routes

Open config/routes.rb and check the routes Devise created for you:

# config/routes.rb
devise_for :users

This single line gives you all the authentication routes: /users/sign_up, /users/sign_in, /users/sign_out, and more.

🔑 Protecting Actions

To require a signed-in user before creating a post, add before_action :authenticate_user! to the controller:

# app/controllers/posts_controller.rb
class PostsController < ApplicationController
  before_action :authenticate_user!, except: [:index, :show]

  # ... existing actions ...
end

Now a visitor who is not signed in will be redirected to the login page when they try to create, edit, or delete a post.

👤 Showing the Current User

Devise provides a current_user helper. You can use it in your views to show the signed-in user’s email or name:

<% if user_signed_in? %>
  <p>Welcome, <%= current_user.email %>!</p>
  <%= link_to "Sign Out", destroy_user_session_path, data: { turbo_method: :delete } %>
<% else %>
  <%= link_to "Sign In", new_user_session_path %> |
  <%= link_to "Sign Up", new_user_registration_path %>
<% end %>

🏠 Practice Exercises

# 1. Add the gem and install
gem install rails
# (add gem "devise" to Gemfile first)
bundle install
rails generate devise:install

# 2. Create the User model
rails generate devise User
rails db:migrate
# 3. Protect the posts controller
class PostsController < ApplicationController
  before_action :authenticate_user!, except: [:index, :show]
end

Now try signing up at /users/sign_up, then log in and try creating a post. Try creating a post while logged out and you will be redirected to the login page. Authentication is a huge topic, and Devise handles all the secure parts for you – passwords are hashed, sessions are managed, and reset tokens work out of the box. In the final chapter you will put everything together in a complete capstone project.

Leave a Reply

Your email address will not be published. Required fields are marked *