Friday, 12 October 2012

Devise Details

Devise usages for rails 3 application:

Devise is very simple authentication solution for Rails 3 application.
There are many gems for authenticating a rails application. But, "devise" is the gem which will make authentication of a rails 3 application easier.

1) Include devise gem in Gemfile as following,

gem ‘devise’

2)Run the below command

bundle install

3) Run generator as following,

rails g devise:install

4) Create model ‘User’ to store authentication

rails g devise User

5)To make the view files we have to give the following command

rails g devise:views

6)For database creation

rake db:create
rake db:migration

7)In layouts applicaiton.html.erb:

<div id="user_nav">
<% if user_signed_in? %>
Signed in as <%= current_user.email %>. Not you?
<%= link_to "Sign out", destroy_user_session_path %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or <%= link_to "sign in", new_user_session_path %>
<% end %>
</div>


And also add the following code to the same html file in body tag:

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

8) Add the following code to the "config/environments/development.rb" file:

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

9) Now we can create our own controller which, we need this authentication.

rails g controller home

10) if we want to authenticate this controller’s action means we have to give like this,

before_filter :authenticate_user!

11) In routes,(config/routes.rb)

root :to => "home#index"

Whenever you enter the home controller action in url means it will authenticate.

References:
https://github.com/plataformatec/devise
http://www.rorexperts.com/authentication-of-rails-3-application-using-devise-gem-t2243.html
http://www.allerin.com/blog/devise-usage-in-rails-3-devise-authentication-in-ruby-on-rails-3-application/


Omniauth gem for facebook and twitter integration:

1.In gem files
gem 'omniauth'

2.bundle install
rails g scaffold authentication user_id:integer provider:string uid:string index create destroy
rake db:migrate


3.models/user.rb
has_many :authentications

4.models/authentications.rb
belongs_to :user

5.config/routes.rb

match '/auth/:provider/callback' => 'authentications#create'
6.authentications_controller.rb

def index
 @authentications = current_user.authentications if current_user
end

def create
 auth = request.env["rack.auth"]
 current_user.authentications.find_or_create_by_provider_and_uid(auth['provider'], auth['uid'])
 flash[:notice] = "Authentication successful."
 redirect_to authentications_url
end

def destroy
 @authentication = current_user.authentications.find(params[:id])
 @authentication.destroy
 flash[:notice] = "Successfully destroyed authentication."
 redirect_to authentications_url
end


7.config/initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
 provider :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET'

 provider :facebook, 'APP_ID', 'APP_SECRET'
 #provider :linked_in, 'CONSUMER_KEY', 'CONSUMER_SECRET'
end


8.authentications/index.html.erb

<% title "Sign In" %>

<% if @authentications %>
 <% unless @authentications.empty? %>
   <p><strong>You can sign in to this account using:</strong></p>
   <div class="authentications">
     <% for authentication in @authentications %>
       <div class="authentication">
         <%= image_tag "#{authentication.provider}_32.png", :size => "32x32" %>
         <div class="provider"><%= authentication.provider.titleize %></div>
         <div class="uid"><%= authentication.uid %></div>
         <%= link_to "X", authentication, :confirm => 'Are you sure you want to remove this authentication option?', :method => :delete, :class => "remove" %>
       </div>
     <% end %>
     <div class="clear"></div>
   </div>
 <% end %>
 <p><strong>Add another service to sign in with:</strong></p>
<% else %>
 <p><strong>Sign in through one of these services:</strong></p>
<% end %>

<a href="/auth/twitter" class="auth_provider">
 <%= image_tag "twitter_64.png", :size => "64x64", :alt => "Twitter" %>
 Twitter
</a>
<a href="/auth/facebook" class="auth_provider">
 <%= image_tag "facebook_64.png", :size => "64x64", :alt => "Facebook" %>
 Facebook
</a>
<div class="clear"></div>


9.For stylesheets (application.css)

.authentications {
 margin-bottom: 30px;
}
.authentication {
 width: 130px;
 float: left;
 background-color: #EEE;
 border: solid 1px #999;
 padding: 5px 10px;
 -moz-border-radius: 8px;
 -webkit-border-radius: 8px;
 position: relative;
 margin-right: 10px;
}
.authentication .remove {
 text-decoration: none;
 position: absolute;
 top: 3px;
 right: 3px;
 color: #333;
 padding: 2px 4px;
 font-size: 10px;
}
.authentication .remove:hover {
 color: #CCC;
 background-color: #777;
 -moz-border-radius: 6px;
 -webkit-border-radius: 6px;
}
.authentication img {
 float: left;
 margin-right: 10px;
}
.authentication .provider {
 font-weight: bold;
}

.authentication .uid {
 color: #666;
 font-size: 11px;
}
.auth_provider img {
 display: block;
}
.auth_provider {
 float: left;
 text-decoration: none;
 margin-right: 20px;
 text-align: center;
 margin-bottom: 10px;
}


Reference link:

screenshot details:
http://railscasts.com/episodes/235-omniauth-part-1

http://stackoverflow.com/questions/11093120/devise-omniauth-and-facebook-integration-session-error

http://www.phyowaiwin.com/how-to-download-and-display-twitter-feeds-for-new-year-resolution-using-ruby-on-rails

http://railscasts.com/episodes/235-omniauth-part-1

No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...