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
In this blog i’ll show you the basic integration of Devise in a Rails app.Below are the steps to follow:
Add the gem in your gem file
Run the “bundle install” command to install the gem.
Define your root url like below
root :to => “home#index”
Now you need to run the generator command
rails generate devise:install
This generator will install all Devise configurations.Take a look at them.
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.
Run the migrate command to insert the User table in your database.
It’ll insert the Users table with some columns.
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.