Saturday 24 March 2012

DEVISE – Authentication GEM for a Rails app

Comprises of 12 modules :

Database Authenticatable
Token Authenticatable
Omniauthable
Confirmable
Recoverable
Registerable
Rememberable
Trackable
Timeoutable
Validatable
Lockable
Encryptable

STEPS TO FOLLOW
https://github.com/plataformatec/devise

 1. Create project :
      rails new project_name

2. Create database :
      rake db:create

3. Gem ‘devise’  (paste it in gem file)

4. Bundle install

5. Generate scaffold :
      rails g scaffold name  attributes

6. Specify the route in routes.rb -> root:to => “name#index“

7. Migrate it :
     rake db:migrate

8. Rails generate devise:install

9. In config -> environments ->development -> add this line
     config.action_mailer.default_url_options = { :host => 'localhost:3000' }

10. In views ->application.html.erb -> paste some lines to create session

=> To create session :
<% 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 %>
      </div>
      <% flash.each do |name, msg| %>
        <%= content_tag :div, msg, :id => "flash_#{name}" %>
      <% end %>

11. Rails g devise User

12. Do some modifications in migration file of User based on our attributes
      and requirements.

13. Again do migration

14. Rake routes

15. Add filter in controller
      before_filter :authenticate_user!, :except => [:show, :index]

16. Rails g devise:views  ( to include views in our application)

17. In new.html.erb -> Add custom fields in registration

18. All error messages will be stored in
      config -> locales ->devise.en.yml

19. Config -> initializers ->devise.rb
     Uncomment ‘ config.encrypt:sha512 ‘

20. config -> development.rb -> do smtp settings  to send email
    config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
  :enable_starttls_auto  => true, 
  :address                      => 'smtp.gmail.com',
  :port                             => 587,
  :tls                                => true,
  :domain                        => 'gmail.com',
  :authentication             => :plain,
  :user_name                  => ‘xyz@gmail.com',
  :password                     => ‘xxxxxx'
}
end


Now start your server and work on secured application.     
     

No comments:

Post a Comment