I think that from the title you can pretty much guess what this post will be about, specially if you are familiar with these magic words: Rails, Facebooker, Facebook Connect. Ok they are not that magic, but you can do lots of fun stuff with them. In case you don’t live in the facebook planet I will briefly explain them, since there is a lot of info out there about them I wont get into detail. I won’t explain Rails for obvios reaons.
It’s something (who knows what and who cares anyway?) from Facebook that allows you to use your facebook login to login into other web sites.
It’s a gem for Ruby, and also a plugin for Rails that converts the results from the facebook API into ruby objects so you can interact with the API using just ruby.
Let the magic begin…
We want to have a site that handles users like we would normally do, except we won’t handle nor model them. We will get them from facebook. The first thing we want to do is join the developers group in facebook, go to https://www.facebook.com/developers/ and create a new application. You will then have to configure some couple of things, the main one is “Connect URL” in which you should put the url of your site like: “https://my_new_fb_app.com/”. Do not forgett the last slash, it won’t work if it’s not there (and you can spend hours trying to figure out what’s wrong). You have many other setting but we won’t get to them here.
As an advice, you may want to create two apps so that you can have one for development and one for production. the development one for example can point to http://localhost:3000/
Other thing you may see is that facebook gives you some strange numbers after you create your app, now is when we start with facebooker. Follow this tutorial until point 5, we won’t care too much for the other stuff, but you can read it. I want you to know how to install facebooker (and to actually do it), and then generate and complete the facebooker.yml file.
After you have that completed the first thing we wan’t is to show this little pretty blue button . This is the login button, and when you click it a pop up from facebook asking your password and email should appear. So, let’s make that happen.
Add this to your application controller
Add the followng lines to one of your views (typically a login page)
<%= init_fb_connect "XFBML"%>
<%= fb_login_and_redirect(facebook_login_users_path) %>
Now you should be seeing the button after you refresh. The facebook_login_users_path is the url you want to redirect your users after they login. As usual in Rails facebook_login is the action and users the controller.
After the user logges in you will have a facebook_session variable abailable to do almost whatever you want. For example you can:
facebook_session.user, this will return you the facebook user and all of it’s methods. Everything you can do with it it’s here https://facebooker.rubyforge.org/classes/Facebooker/User.html. The typicall things you may want are facebook_session.user.first_name, facebook_session.user.last_name, facebook_session.user.first_name.id (this will return an id facebook provides. You can access with it the user any other time).
Other interesting thins you can do is facebook_session.friends to get all the user friends or facebook_session.user.friends_with_this_app will return all the users that use this application and are already your friends in facebook.