Wednesday, 30 April 2014

AUTHENTICATION USING DEVISE GEM

AUTHENTICATION USING DEVISE GEM

1, create a new application (Here my application name is deviseauth).

2, Remove index file from public and set the root file, (Here my root file is    
root :to => 'posts#home')

3, Add below statement to Gemfile.
gem 'devise'

4, Run $bundle install command.

5, Run $rails generate devise:install
     The generator will install an initializer which describes ALL Devise's configuration options and you M,UST take a look at it. When you are done, you are ready to add Devise to any of your models using the generator.

6, $rails generate devise MODELNAME
    $rails generate devise user #Here user is modelname

7, Next u vil usally run $rake db:migrate

8, Add below filter to application_contoller.rb
  before_filter :authenticate_user!

9, Run the server
 $ rails s
10, Open localhost:3000 in ur browser u vill get below output.


11, to allow flash messages paste the below code in /layouts/application.html.erb
       <p class="notice"><%= notice %></p>
        <p class="alert"><%= alert %></p>

current_user.email:

The current signed-in user email.

code in view:
Welcome <%= current_user.email %> !!!

user_signed_in?:

To verify if a user is signed in, use the above helper. Returns true/false.

SESSION EXPIRING:

user.rb

class User
 devise  :timeoutable
 def timeout_in
   1.minutes
  end
end

SIGN OUT:

 <%= link_to "Sign Out",destroy_user_session_path,:method => :delete if user_signed_in? %>


No comments:

Post a Comment