Setting Up User Authentication With Devise



I am starting work on a new application . I wanted it to have multiple users, but I didn't want to reinvent the wheel, so I decided to use a pre-existing gem called Devise. I am on a Mac using Ruby 2 , 3 and Rails 4.


Add Devise

If you want to run Bootstrap and/or Simple Form on your app, please follow the instructions on my blog post Bootstrap Your App before proceeding.
I selected Devise simply because it was the most popular Rails Authentication gem at The Ruby Toolbox and I wanted to try it out. Add the devise gem to your Gemfile:


Gemfile
1
gem 'devise'

Next run install:


1
$ bundle install

Taking a look at the Getting Started documentation, I followed the next steps. Run the generator:


1
$ rails generate devise:install

Doing this also resulted in some intructions from Devise that I followed. I first defined the default url options in config/environments/development.rb by adding this line:


config/environments/development.rb
1
config.action_mailer.default_url_options = { :host => 'localhost:3000' }

I also add flash message display in my app/views/layouts/application.html.erb file by adding these lines in the <body> tag:


app/views/layouts/application.html.erb
1
2
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

Next, copy Devise views (for customization) to your app by running:


1
$ rails g devise:views

Next create a user model and configure it with default Devise modules:


1
$ rails generate devise USER

Next create the database and table:


1
2
$ rake db:create:all
$ rake db:migrate

Add sign in, sign out and sign up links to your layout template

These instructions are part of the Devise documentation.
First add sign in/out links, so the appropriate one will show up depending on whether the user is already signed in. Create file app/views/devise/menu/_login_items.html.erb with the following code:


app/views/devise/menu/_login_items.html.erb
1
2
3
4
5
6
7
8
9
<% if user_signed_in? %>
  <li>
  <%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
  </li>
<% else %>
  <li>
  <%= link_to('Login', new_user_session_path)  %>
  </li>
<% end %>

Add sign up links that are substituted with something else useful if the user is already signed in. Create file app/views/devise/menu/_registration_items.html.erb with the following code:


app/views/devise/menu/_registration_items.html.erb
1
2
3
4
5
6
7
8
9
<% if user_signed_in? %>
  <li>
  <%= link_to('Edit registration', edit_user_registration_path) %>
  </li>
<% else %>
  <li>
  <%= link_to('Register', new_user_registration_path)  %>
  </li>
<% end %>

Then render these templates in your app/views/layouts/application.html.erb file. You could put them in the middle of a navigation bar, something like this:


app/views/layouts/application.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
<nav role="navigation">
    <div class="inner">
        <a href="#nav" class="nav-collapse" id="nav-collapse">Menu</a>
        <ul class="nav" id="nav">
            <li><%= link_to "Home", root_path %></li>
            <%= render 'devise/menu/registration_items' %>
            <%= render 'devise/menu/login_items' %>
            <li><%= link_to "About", about_path %></li>
            <li><%= link_to "Contact", contact_path %></li>
        </ul>
    </div>
</nav>

Then add some CSS to style this into a proper navigation bar. And continue on to make the rest of your application. Before you get too far, you might want to deploy to Heroku just to make sure everything is working with the users.

Comments

Popular posts from this blog

how to customize dashboard in active admin gem in rails 4

fibonacci series in Ruby

Simple calculator using JQuery and HTML..