Ruby on Rails / Authentication and Authorization

Setting Up User Authentication with Devise

This tutorial will guide you through the process of setting up user authentication in a Rails application using Devise. Devise is a flexible and widely-used authentication solutio…

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Covers implementing user authentication and role-based authorization in Rails.

1. Introduction

Goal of the Tutorial

This tutorial aims to guide you on how to set up user authentication in your Rails application using Devise. User authentication is a critical component of most web applications and Devise makes it easy to build this functionality into your Rails projects.

Learning Outcomes

By the end of this tutorial, you will have a Rails application with user authentication enabled. You will learn how to integrate Devise into your application, create user sign-up and sign-in functionality, and manage user sessions.

Prerequisites

Familiarity with Ruby on Rails and basic understanding of MVC architecture is required. You should also have Ruby on Rails installed on your local machine.

2. Step-by-Step Guide

Add Devise to Your Rails Application

First, you need to add the Devise gem to your Rails application. In your Gemfile, add the following line:

gem 'devise'

Then, run bundle install to install the gem.

Generate Devise Model

Devise works by generating a model (typically User) that handles user authentication. Generate this model by running:

rails generate devise User

This command will create a User model and configure it with default Devise modules.

Run Migrations

After generating the User model, you need to update your database schema to include the new model. Run this command:

rails db:migrate

Configure Routes

Devise also generates routes for user sign-up, sign-in, and other related functions. You can see these routes by running rails routes.

Protecting Resources

To protect certain resources from non-authenticated users, you can use the authenticate_user! before_action in your controllers:

before_action :authenticate_user!

3. Code Examples

Creating a Sign-Up Page

Devise creates all views needed for authentication but they are hidden in the gem. To customize these views, you can generate them with this command:

rails generate devise:views

After running this command, you'll find the sign-up form in app/views/devise/registrations/new.html.erb.

Signing-In and Signing-Out

In your application layout (app/views/layouts/application.html.erb), you can add links for users to sign-in and sign-out:

<% if user_signed_in? %>
  <%= link_to 'Sign out', destroy_user_session_path, method: :delete %>
<% else %>
  <%= link_to 'Sign in', new_user_session_path %>
<% end %>

This code checks if a user is signed in. If so, it displays a 'Sign out' link. Otherwise, it displays a 'Sign in' link.

4. Summary

In this tutorial, you've learned how to add user authentication to a Rails application using Devise. You've learned how to install Devise, generate a User model, run migrations, and protect resources. You've also learned how to customize Devise views and manage user sessions.

For further learning, you could explore other Devise modules like Confirmable and Lockable. You could also look into how to customize the User model to include additional fields.

5. Practice Exercises

  1. Add a 'Sign up' link to the application layout.
  2. Customize the sign-up form to include a 'First name' and 'Last name' field.
  3. Add a feature where users can only view a page if they are signed in.

Remember, the key to mastering a new skill is practice!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

Image Converter

Convert between different image formats.

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Random String Generator

Generate random alphanumeric strings for API keys or unique IDs.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help