Authentication Crash Course with Devise

Episode #45 by Teacher's Avatar David Kimura


Devise is a flexible authentication solution for Rails based on Warden. Learn how to add a stable authentication solution like Devise to your application.
rails security authentication 8:04



# Gemfile
gem 'devise'

If you want to use the bootstrap views, add the following to your gem file. Run bundle and and the generator.

# Bootstrap Views
gem 'devise-bootstrapped', github: 'king601/devise-bootstrapped'

# Bash

rails generate devise:views:bootstrapped

# Bash
rails generate devise:install
rails generate devise User # MODEL
rake db:migrate

# _navigation_links.html.erb
<li><%= link_to 'Protected', protected_path %></li>
<% if user_signed_in? %>
  <li><%= link_to, edit_user_registration_path %></li>
  <li><%= link_to 'Logout', destroy_user_session_path, method: :delete %></li>
<% else %>
  <li><%= link_to 'Login', new_user_session_path %></li>
<% end %>

# development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

# routes.rb
Rails.application.routes.draw do
  devise_for :users
  get :protected, to: 'visitors#protected'
  root 'visitors#index'

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