There are lots of Facebook + Rails solutions, most notably Facebooker, but this and many others are not compatible with Rails 3. I'm currently using Authlogic for authentication with my app, and I want to give users the option of Facebook to sign in. I want to find the best way to have FB and Authlogic go together; right now I'm just writing my own Authlogic add-on for Facebook but if this has already been done then I don't want to redo someone else's work. Does anyone know of anything like this?
This area seems to be heavily in flux. I had a difficult time getting it set up with Rails 3 since none of the main Ruby facebook libraries are officially updated for it yet, and the newest ones aren't documented.
The way I got it working with Rails 3 was using Facebooker2 and Mogli. I basically took the mogli + authlogic oauth controller code posted here and combined it with Facebooker2's controller and helper methods. There's no documentation (that I know of) for Facebooker2 and Mogli, but the Facebooker2 code is relatively straightforward.
I also had to manually deal with some of the authlogic validations in order to have them trigger only for non-facebook signups. Here is all the authlogic-specific code in the user model:
class User < ActiveRecord::Base
acts_as_authentic do |config|
config.validate_email_field = false
end
validates_length_of :email, :within => 6..100, :if => :is_not_fb_user?
validates_format_of :email, :with => Authlogic::Regex.email, :if => :is_not_fb_user?
validates_uniqueness_of :email, :if => :is_not_fb_user?
def is_fb_user?
return false if self.fb_uid.blank? || self.fb_uid == 0 || self.fb_at.blank?
return true
end
def is_not_fb_user?; !self.is_fb_user? end
def require_password?
if self.is_fb_user?
false
else
new_record? || password_changed? || send(crypted_password_field).blank?
end
end
end
There might be some bugs and I suspect there is a better way to approach this.
A friend has developed this gem for our application, so maybe it's useful on your app too: http://github.com/pfracarolli/facebook-graph.