Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Extension of the Authlogic library to add Facebook Connect support built upon the excellent facebooker gem
branch: master

This branch is 28 commits ahead, 2 commits behind kalasjocke:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
README.rdoc
init.rb

README.rdoc

Help make these instructions better, submit your improvements via github.

This repo is forked and merged from :

  1. github.com/kalasjocke/authlogic_facebook_connect

  2. github.com/james2m/authlogic_facebook_connect

  3. github.com/mattenat/authlogic_facebook_connect

  4. github.com/eliotsykes/authlogic_facebook_connect

This plugin is ready for production.

Install and use

1. Install facebooker as gem or plugin

You can install facebooker as a gem or as a plugin.

If you want to install facebooker as a gem

$ sudo gem install facebooker

Make your application dependent on the facebooker gem

config.gem "facebooker"

If you want to install facebooker as a plugin

$ script/plugin install git://github.com/mmangino/facebooker.git

Once you've installed facebooker gem or plugin====

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

Copy the template facebooker.yml from the facebooker gem source to your rails app at config/facebooker.yml.

Next create a facebook application for your development environment at www.facebook.com/developers/createapp.php

Once you've created your Facebook app, you will be shown an api_key and a secret_key. These should be copied into the development section of config/facebooker.yml. In facebooker.yml also set the callback_url to the home page of your development app, e.g. localhost:3000.

Add the following 2 lines to your application(_controller).rb (ApplicationController):

# Facebooker docs recommend this to prevent Facebook TOS violation.
filter_parameter_logging :fb_sig_friends

For more information on the facebooker gem checkout it's readme 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
    add_column :users, :facebook_session_key, :string
  end

  def self.down
    remove_column :users, :facebook_session_key
    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 in the opening html tag

  • The javascript include call, fb_connect_javascript_tag

  • The init_fb_connect call.

    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns=“www.w3.org/1999/xhtml” xmlns:fb=“www.facebook.com/2008/fbml”> <head>

    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <title>Your awesome facebook connected app</title>
    
    <%= stylesheet_link_tag 'application' %>
    <%= javascript_include_tag :defaults %>

    </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 %>

If the path to create an authlogic session is not mapped to 
the /user_session route then you will need to pass in a :controller option,
e.g.

<%= authlogic_facebook_login_button :controller => 'path_to_create_session' %>

If you are using jquery instead of the default prototype library, then:

<%= authlogic_facebook_login_button :js => :jquery %>

You can also pass in additional options to authlogic_facebook_login_button that
will be passed on to facebooker's fb_login_button method to alter behaviour
and formatting of the button, e.g.:

<%= authlogic_facebook_login_button :size => 'large',
  :length => 'long', :autologoutlink => true, :v => 2 %>

Notes

before_connect hook

If you want to save some user data when connecting to facebook you can define the before_connect hook in your user model. before_connect is called the first time a facebook user authenticates with your application.

This hook is a good place to set values for authlogic required credentials or you can make credentials optional by allowing nulls in the database. See the authlogic example app migration for more on optional credential columns.

The only credential column that is not optional is persistence token. You can ensure this is set in before_connect with reset_persistence_token.

Here is an example implementation of the before_connect hook:

def before_connect(facebook_session)
  self.name = facebook_session.user.name
  # Set required credentials
  reset_persistence_token
end

For more information about what you can get form the facebook_session checkout the Facebooker gem rdoc.

Testing with cucumber

If you want to test with cucumber, you have to add cucumber environtment in the facebooker.yml. Example:

development:
  api_key: YOUR API KEY
  secret_key: YOUR SECRET KEY
  canvas_page_name: Bukalapak
  callback_url:
  pretty_errors: true
  set_asset_host_to_callback_url: true
  #......

test:
  api_key: YOUR API KEY
  secret_key: YOUR SECRET KEY
  canvas_page_name:
  callback_url:
  set_asset_host_to_callback_url: true
  #......

production:
  api_key: YOUR API KEY
  secret_key: YOUR SECRET KEY
  canvas_page_name:
  callback_url: 
  set_asset_host_to_callback_url: true   
  #......

cucumber:
  api_key: YOUR API KEY
  secret_key: YOUR SECRET KEY
  canvas_page_name:
  callback_url: 
  set_asset_host_to_callback_url: true   
  #......

Help make these instructions better, submit your improvements via github.

Something went wrong with that request. Please try again.