Permalink
Browse files

fbook plugins

  • Loading branch information...
1 parent c30cec3 commit 090ec94d3d4ce27c8654072715b6a34c9b947134 Aashish Manchanda committed Jul 7, 2009
@@ -1,2 +1,2 @@
module AppConfigsHelper
-end
+end
@@ -0,0 +1,11 @@
+class AddFacebookConnectFieldsToUser < ActiveRecord::Migration
+ def self.up
+ add_column :users, :name, :string
+ add_column :users, :facebook_uid, :integer, :limit => 8
+ end
+
+ def self.down
+ remove_column :users, :facebook_uid
+ remove_column :users, :name
+ end
+end
View
@@ -0,0 +1,10 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" >
+<head>
+ <title>Cross-Domain Receiver Page</title>
+</head>
+<body>
+ <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/XdCommReceiver.debug.js" type="text/javascript"></script>
+</body>
+</html>
No changes.
@@ -0,0 +1,68 @@
+== Install and use
+
+=== 1. Install the facebooker gem and make Rails use it
+
+ $ sudo gem install facebooker
+
+Make sure that you have setup you config/facebooker.yml to match your facebook application, make you application depended on the facebooker gem
+
+ config.gem "facebooker"
+
+and run the xd_receiver generator to create the cross domain scripting bridge to make it possible for your application to communicate with facebook
+
+ $ script/generate xd_receiver
+
+For more information on the facebooker gem checkout it's readme http://github.com/mmangino/facebooker/tree/master
+
+=== 2. Install the Authlogic Facebook Connect plugin
+
+ $ script/plugin install git://github.com/kalasjocke/authlogic_facebook_connect.git
+
+This plugin should soon be packed inside a gem.
+
+=== 3. Make some changes to your database
+
+ class AddFacebookConnectFieldsToUser < ActiveRecord::Migration
+ def self.up
+ add_column :users, :name, :string
+ add_column :users, :facebook_uid, :integer, :limit => 8
+ end
+
+ def self.down
+ remove_column :users, :facebook_uid
+ remove_column :users, :name
+ end
+ end
+
+=== 4. Make your layout look something like this
+
+The important parts are the facebook html namespace, facebook javascript include and the facebook helper calls to init the facebooker gem.
+
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+ <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
+ <head>
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+ <title>Your awsome facebook connected app</title>
+
+ <%= stylesheet_link_tag 'application' %>
+ <%= javascript_include_tag :defaults %>
+
+ <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
+ </head>
+ <body>
+
+ <p style="color: green"><%= flash[:notice] %></p>
+
+ <%= fb_connect_javascript_tag %>
+ <%= init_fb_connect "XFBML" %>
+
+ <%= yield %>
+
+ </body>
+ </html>
+
+=== 5. Add the Facebook Connect button to your login form
+
+ <%= authlogic_facebook_login_button %>
@@ -0,0 +1 @@
+require 'authlogic_facebook_connect'
@@ -0,0 +1,8 @@
+# require "authlogic_facebook_connect/version"
+require "authlogic_facebook_connect/acts_as_authentic"
+require "authlogic_facebook_connect/session"
+require "authlogic_facebook_connect/helper"
+
+ActiveRecord::Base.send(:include, AuthlogicFacebookConnect::ActsAsAuthentic)
+Authlogic::Session::Base.send(:include, AuthlogicFacebookConnect::Session)
+ActionController::Base.helper AuthlogicFacebookConnect::Helper
@@ -0,0 +1,20 @@
+module AuthlogicFacebookConnect
+ module ActsAsAuthentic
+ def self.included(klass)
+ klass.class_eval do
+ extend Config
+ add_acts_as_authentic_module(Methods, :prepend)
+ end
+ end
+
+ module Config
+ end
+
+ module Methods
+ def self.included(klass)
+ klass.class_eval do
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,22 @@
+module AuthlogicFacebookConnect
+ module Helper
+ def authlogic_facebook_login_button(options = {})
+ # TODO: Make this with correct helpers istead of this uggly hack.
+
+ options[:controller] ||= "user_session"
+
+ output = "<form id='connect_fo_facebook_form' method='post' action='/#{options[:controller]}'>\n"
+ output += "<input type='hidden' name='authenticity_token' value='#{form_authenticity_token}'/>\n"
+ output += "</form>\n"
+ output += "<script type='text/javascript' charset='utf-8'>\n"
+ output += " function connect_to_facebook() {\n"
+ output += " $('connect_fo_facebook_form').submit();\n"
+ output += " }\n"
+ output += "</script>\n"
+
+ output += fb_login_button("connect_to_facebook()")
+
+ output
+ end
+ end
+end
@@ -0,0 +1,74 @@
+module AuthlogicFacebookConnect
+ module Session
+ def self.included(klass)
+ klass.class_eval do
+ extend Config
+ include Methods
+ end
+ end
+
+ module Config
+ # What user field should be used for the facebook UID?
+ #
+ # This is useful if you want to use a single field for multiple types of
+ # alternate user IDs, e.g. one that handles both OpenID identifiers and
+ # facebook ids.
+ #
+ # * <tt>Default:</tt> :facebook_uid
+ # * <tt>Accepts:</tt> Symbol
+ def facebook_uid_field(value = nil)
+ rw_config(:facebook_uid_field, value, :facebook_uid)
+ end
+ alias_method :facebook_uid_field=, :facebook_uid_field
+ end
+
+ module Methods
+ def self.included(klass)
+ klass.class_eval do
+ validate :validate_by_facebook_connect, :if => :authenticating_with_facebook_connect?
+ end
+
+ def credentials=(value)
+ # TODO: Is there a nicer way to tell Authlogic that we don't have any credentials than this?
+ values = [:facebook_connect]
+ super
+ end
+ end
+
+ def validate_by_facebook_connect
+ facebook_session = controller.facebook_session
+
+ self.attempted_record =
+ klass.find(:first, :conditions => { facebook_uid_field => facebook_session.user.uid })
+
+ unless self.attempted_record
+ begin
+ # Get the user from facebook and create a local user.
+ #
+ # We assign it after the call to new in case the attribute is protected.
+ new_user = klass.new
+ new_user.send(:"#{facebook_uid_field}=", facebook_session.user.uid)
+ self.attempted_record = new_user
+
+ errors.add_to_base(
+ I18n.t('error_messages.facebook_user_creation_failed',
+ :default => 'There was a problem creating a new user ' +
+ 'for your Facebook account')) unless attempted_record.valid?
+ rescue Facebooker::Session::SessionExpired
+ errors.add_to_base(I18n.t('error_messages.facebooker_session_expired',
+ :default => "Your Facebook Connect session has expired, please reconnect."))
+ end
+ end
+ end
+
+ def authenticating_with_facebook_connect?
+ attempted_record.nil? && errors.empty? && controller.facebook_session
+ end
+
+ private
+ def facebook_uid_field
+ self.class.facebook_uid_field
+ end
+ end
+ end
+end

0 comments on commit 090ec94

Please sign in to comment.