Authentication Using Devise In Rails 3

Devise provides a flexible authentication solution for any Rails app. Devise is a Rails Engine and covers controllers and views as well.It composed of 10 modules such as:

  • Database Authenticatable
  • Omniauthable
  • Confirmable
  • Recoverable
  • Registerable
  • Rememberable
  • Trackable
  • Timeoutable
  • Validatable
  • Lockabl

In this blog i’ll show you the basic integration of Devise in a Rails app.Below are the steps to follow:

Step:1
Add the gem in your gem file
gem ‘devise’
Run the “bundle install” command to install the gem.

Step:2
Define your root url like below
root :to => “home#index”

Step:3
Now you need to run the generator command
rails generate devise:install
This generator will install all Devise configurations.Take a look at them.

Step:4
After done with the above options,you are ready to add Devise to any of your models using the generator:
rails generate devise User
This generator creates a few interesting things: a model file, a migration and a devise_for route.

Step:5
Run the migrate command to insert the User table in your database.
rake db:migrate
It’ll insert the Users table with some columns.

Step:6
Add the following block in your application’s layout file(application.html.erb)
<% if user_signed_in? %>
Signed in as <%= current_user.email %>. Not you?
<%= link_to “Sign out”, destroy_user_session_path,:method => :delete %>
<% else %>
<%= link_to “Sign up”, new_user_registration_path %> or
<%= link_to “Sign in”, new_user_session_path %>
<% end %>

Now when you reload the page ,you can see a sign in/sign up links.In signup page initially there are only Email,Password and Confirm Password feilds present.You can add your custom fields by overriding the devise view files by running the following command in your terminal:
rails generate devise:views

Now your application is configured with Devise authentication.

Advertisements

6 thoughts on “Authentication Using Devise In Rails 3

  1. I’m now not certain where you are getting your information, but great topic.
    I must spend a while finding out more or figuring out more.
    Thank you for wonderful information I was looking for this information for my mission.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s