Authentication with Devise

November 17, 2019

In this tutorial we will see how you can authenticate the RUBY on Rails application with Devise gem


Step 1
Add the following code to youe Genfile
gem 'devise'

Step 2
Next, you run the following command
rails generate devise:install
This will make your application ready for Device

Some setup you must do manually if you haven’t yet:

1. Ensure you have defined default url options in your environments files. Here
is an example of default_url_options appropriate for a development environment
in config/environments/development.rb:

config.action_mailer.default_url_options = { host: ‘localhost’, port: 3000 }

In production, :host should be set to the actual host of your application.

2. Ensure you have defined root_url to *something* in your config/routes.rb.
For example:

root to: “home#index”

3. Ensure you have flash messages in app/views/layouts/application.html.erb.
For example:

<%= notice %>

<%= alert %>

4. You can copy Devise views (for customization) to your app by running:

rails g devise:views

Step 3
Now we will crate the User model
rails g devise user

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

Step 4
Then you can run the migrate command

This will generate following routes in your application and also devise_for :users will be added to congfig\routes.rb file

                     new_user_session GET    /users/sign_in(.:format)                                                                 devise/sessions#new
                         user_session POST   /users/sign_in(.:format)                                                                 devise/sessions#create
                 destroy_user_session DELETE /users/sign_out(.:format)                                                                devise/sessions#destroy
                    new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
                   edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
                        user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                                      PUT    /users/password(.:format)                                                                devise/passwords#update
                                      POST   /users/password(.:format)                                                                devise/passwords#create
             cancel_user_registration GET    /users/cancel(.:format)                                                                  devise/registrations#cancel
                new_user_registration GET    /users/sign_up(.:format)                                                                 devise/registrations#new
               edit_user_registration GET    /users/edit(.:format)                                                                    devise/registrations#edit
                    user_registration PATCH  /users(.:format)                                                                         devise/registrations#update
                                      PUT    /users(.:format)                                                                         devise/registrations#update
                                      DELETE /users(.:format)                                                                         devise/registrations#destroy
                                      POST   /users(.:format)  

Where are your template files?

How about the template files for sign up, sign in etc? Where are they located?