2 septembre 2009

Facebook Connect and Rails: handling logout from Facebook site in Facebooker

Integrating an existing website with Facebook Connect using Facebooker is fairly easy. Several tutorial have been made; the best I found being:However, there is no description on how to handle a frequent use case where the user has been authentified in your application using Facebook Connect and disconnects in the Facebook website.

In this case, you will surely meet the famous:

Facebooker::Session::SessionExpired (Session key invalid or no longer valid)

One solution to handle this is rescue_from introduced in Rails 2.0. This is also the method recommended in the Facebooker documentation.

When the exception occurs, you have to remove the facebook session informations AND facebooker cookies.
Indeed, the set_facebook_session will recreate the facebook_session with these cookies. If you do not remove them, you will loop again and again in your rescue_from handling.

Here is an example of what you can do:

class ApplicationController < ActionController::Base
...
rescue_from Facebooker::Session::SessionExpired do |exception|
clear_facebook_session_information
clear_fb_cookies!
reset_session # i.e. logout the user
flash[:notice] = "You have been disconnected from Facebook."
redirect_to root_url
end
...
end


Dead simple, huh?

1 commentaire:

Krstofer a dit…

Ok, what if you are a user and you are getting this error? How do you fix it?

Enregistrer un commentaire