0
0
RailsHow-ToBeginner · 4 min read

How to Implement Authentication in Rails: Simple Guide

To implement authentication in Rails, use the Devise gem which provides ready-made user sign-up, login, and logout features. Install the gem, run its generator, and migrate your database to add authentication to your app quickly and securely.
📐

Syntax

The main steps to add authentication with Devise in Rails are:

  • Add gem 'devise' to your Gemfile.
  • Run bundle install to install the gem.
  • Run rails generate devise:install to set up Devise.
  • Generate a User model with rails generate devise User.
  • Run rails db:migrate to create the users table.
  • Use Devise helper methods like user_signed_in? and current_user in controllers and views.
bash
gem 'devise'

# Then in terminal:
bundle install
rails generate devise:install
rails generate devise User
rails db:migrate
💻

Example

This example shows how to add basic user authentication using Devise in a new Rails app. It creates a User model with email and password, and provides sign up, login, and logout pages automatically.

ruby
# Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 7.0.0'
gem 'devise'

# Terminal commands
bundle install
rails generate devise:install
rails generate devise User
rails db:migrate

# In app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :authenticate_user!
end

# In config/routes.rb
Rails.application.routes.draw do
  devise_for :users
  root to: 'home#index'
end

# Create a simple home controller
rails generate controller home index

# app/views/home/index.html.erb
<% if user_signed_in? %>
  <p>Welcome, <%= current_user.email %>!</p>
  <%= link_to 'Logout', destroy_user_session_path, method: :delete %>
<% else %>
  <%= link_to 'Login', new_user_session_path %> or
  <%= link_to 'Sign up', new_user_registration_path %>
<% end %>
Output
When you visit the root URL, you see login and sign up links if not signed in. After signing up or logging in, you see a welcome message with your email and a logout link.
⚠️

Common Pitfalls

Common mistakes when implementing authentication in Rails with Devise include:

  • Not running rails generate devise:install before generating the User model.
  • Forgetting to run rails db:migrate after generating the User model.
  • Not adding before_action :authenticate_user! in controllers to protect pages.
  • Missing routes for Devise in config/routes.rb.
  • Not configuring mailer settings for password reset emails.
bash
# Wrong: Missing devise install step
rails generate devise User
rails db:migrate

# Right: Always run install first
rails generate devise:install
rails generate devise User
rails db:migrate
📊

Quick Reference

StepCommand / CodePurpose
1gem 'devise' in GemfileAdd Devise gem to your project
2bundle installInstall the gem dependencies
3rails generate devise:installSet up Devise configuration
4rails generate devise UserCreate User model with Devise modules
5rails db:migrateCreate users table in database
6before_action :authenticate_user!Protect controller actions
7devise_for :users in routes.rbAdd Devise routes for users

Key Takeaways

Use the Devise gem for easy and secure authentication in Rails.
Always run 'rails generate devise:install' before creating the User model.
Protect your controllers with 'before_action :authenticate_user!' to require login.
Devise provides ready-made views and helpers for sign up, login, and logout.
Check your routes and migrations carefully to avoid common setup errors.