How to: logout the facebook connect in Ruby on Rails

When connecting your Ruby on Rails application with Facebook Connect, it sometimes a problem to have the user properly logout the Rails application and the Facebook Connect’s session as well.
If we use the plugin Facebooker the logout is mostly a problem and the issue is that the Facebook cookie is not directly removable by reset_session orĀ  logout_killing_session!

It is required to use the Facebook’s method to remove off the Cookie by calling the Facebook’s JavaScript Framework’s method and after that calling the regular logout method of the Rails Application to remove the Rails Application’s session.

Here is how to get it working…

Considering we are using the Restful Authentication plugin and we have the sessions_controller with the destroy method used for logging out the user from the Rails Application.

Ensure you have the following in the file config/routes.rb

map.logout '/logout', :controller => 'sessions', :action => 'destroy'

And the destroy method in the sessions_controller as

def destroy
  clear_facebook_session_informationĀ  # method provided by the Facebooker plugin
  logout_killing_session!
  flash[:notice] = "You have been logged out."
  redirect_to login_path
end

Create a helper method in the file app/helpers/application_helper.rb

def logout_link
  if facebook_session
    return(link_to_function "Log out", "FB.Connect.logoutAndRedirect('/logout')", :href => "logout")
  else
    return(link_to "Log out", logout_url)
end

And in your views wherever you need to provide a Logout link, you can write the code

  <%= logout_link %>


Tags: ,
This entry was posted on Friday, July 30th, 2010 at 6:48 pm and is filed under facebook, rubyonrails. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Your comment