How to Add Authentication to a Rails Web App Using Devise
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
- the basics of Ruby and Rails
- MVC frameworks
- APIs
Also, you must have the following software installed on your PC
- Ruby
- Rails
- A good Text Editor (E.g. Visual Studio Code)
- 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 folderloginApp
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 theloginApp
directory. Add a linegem 'devise'
as shown.