How to Add Authentication to a Rails Web App Using Devise

Nil Madhab
Level Up Coding
Published in
5 min readFeb 9, 2021

--

How to integrate Devise which is a flexible authentication solution for Rails based on Warden.

In this tutorial, we will learn how to add a login based authentication to our Rails Web Application. For adding this authentication feature, we will use a gem (package) called Devise with our Rails Web App.

Pre-Requisites

This tutorial assumes that you are familiar with

  1. the basics of Ruby and Rails
  2. MVC frameworks
  3. APIs

Also, you must have the following software installed on your PC

  1. Ruby
  2. Rails
  3. A good Text Editor (E.g. Visual Studio Code)
  4. A good Browser (E.g. Google Chrome)

We will also use BootStrap for CSS later in the tutorial

What is Devise?

Devise is a flexible authentication solution for Rails based on Warden. It :-

  • is Rack based;
  • is a complete MVC solution based on Rails engines;
  • allows you to have multiple models signed in at the same time;
  • is based on a modularity concept: use only what you really need.

Devise’s README contains all the information you need to get started, and it also touches on its more advanced features.

Let’s now get our hands dirty and begin writing the code

Let’s Code

STEP 1 — Create a Rails App

  • Open a new terminal
  • Execute the command — rails new loginApp . This will a create a new rails app. You will see a new folder loginApp in the directory in which you opened the terminal. This directory is the root directory for our project.

STEP 2 — Add Devise to Rails

  • Now, we will add Devise to rails. To do this, open the file called Gemfile located in the loginApp directory. Add a line gem 'devise' as shown.

--

--