Search code examples
ruby-on-railsruby-on-rails-3facebookfacebook-graph-apikoala

How to create Rails app users from Facebook login using Koala?


I'm new to Rails, and am trying to build a simple app with Facebook integration. I've worked through the Rails tutorials, and I'm pretty comfortable with it as a framework. I'm using the Koala Framework to handle the Facebook stuff.

I have 2 controllers currently, one to handle the app's basic use case, and one for handling user accounts. The basic use case requires user authentication to do anything other than simply browse the site, and I'd like to provide that authentication via Facebook. What I'm looking to do is "register" the user with my app when they log in with Facebook the first time. The only thing my User model defines is a facebook_id field. As far as I can tell, this might require some kind of callback from Facebook to my app to initiate the registration. I think I also need to be able to determine when a user has logged in so that the extended functionality can be provided to them; my understanding is that this can be implemented as a before_filter, so what I have so far is:

# ApplicationController 
def authenticate 
  @facebook_cookies ||= Koala::Facebook::OAuth.new.get_user_info_from_cookie(cookies) 
end

#PacksController - the app's actual functionality 
class PacksController < ApplicationController 
  before_filter :authenticate, :except => [:index, :show] 
  def index 
    .. 
  end 
  def show 
    .. 
  end 
  def new 
    .. 
  end 
  def create 
    .. 
  end 
end 

I've placed the Facebook login button in my application.html.erb following the async example on Facebook itself. I think this might be incorrect, but I'm not completely sure; this doesn't seem to include callback URLs, for example, if that's what I need to use. I've also configured my facebook.yml file as per https://github.com/arsduo/koala/wiki/Koala-on-Rails, and tried adding callback_url parameter too (I don't even know if this is a valid configuration directive for Koala...).

I've read the GitHub wiki and Googled extensively, and I feel I'm stuck. Is what I'm trying to do even possible? Sorry if anything's unclear.


Solution

  • Why don't you use the OmniAuth gem https://github.com/intridea/omniauth/wiki in addition to koala and let OmniAuth handle your user registration?

    Here's a great tutorial on how to go about it http://sleekd.com/tutorials/setting-up-omniauth-authentication-with-facebook/