Skip to content

sowenjub/authlogic_facebook_connect

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 

Repository files navigation

This is the authlogic_facebook_connect updated to work with facebooker2 (and mogli). Tested with rails 2.3.8, authlogic 2.1.5, facebooker2 0.0.5, mogli 0.0.16

Quick notes for those upgrading from facebooker to facebooker 2

Remove the xd_receiver.html and xd_receiver_ssl.html files in your public folder

Update your User model: * change facebook_uid type to string (with integer you can have troubles when using postgresql) * remove facebook_session_key * add facebook_access_token string (or configure the plugin to use the facebook_session_key field to store the facebook_access_token)

Update your config/facebooker.yml file * add an app_id value (see mogli readme for more info) * rename secret_key to secret * leave api_key unchanged * remove all other values (you only need app_id, api_key, secret)

Update your config/routes.rb and add the following line map.oauth_callback “/fb_oauth/create”, :controller=>“user_sessions”, :action=>“create” Define the permissions you want if the default isn’t enough, for example as a constant in your user model. FACEBOOK_SCOPE = ‘email,user_birthday’ (see developers.facebook.com/docs/authentication/permissions for more information)

Remove from your layout/views: * the xmlns:fb=“www.facebook.com/2008/fbml” in your html markup * <script src=“static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php” type=“text/javascript”></script> * <%= fb_connect_javascript_tag %>

	* <%= init_fb_connect "XFBML" %>

Replace your “authlogic_facebook_login_button” buttons with those provided by facebooker2 using the oauth_callback route, like so (the fb_connect_async_js is only needed once per page): <%= fb_connect_async_js %> <%= fb_login_and_redirect(oauth_callback_url, :perms => User::FACEBOOK_SCOPE, :display => ‘popup’) %>

or you can do something a bit more user friendly, for people who are logged on facebook but not on your site. With the code below, they will see their name next to the button.

<%= fb_connect_async_js %> <% if current_facebook_user && current_facebook_user.fetch %> <%= fb_login_and_redirect(oauth_callback_url, :perms => User::FACEBOOK_SCOPE, :display => ‘popup’) %> <%= “ as #{current_facebook_user.first_name} #{current_facebook_user.last_name} (” + fb_logout_link(“change account?”, request.url) + “)” %> <% else %> <%= fb_login_and_redirect(oauth_callback_url, :perms => User::FACEBOOK_SCOPE, :display => ‘popup’) %> <% end %>

Check notes at the bottom for some more info

Install and use

1. Install the facebooker2 and mogli gems

If you don’t use bundler:

$ sudo gem install facebooker2
$ sudo gem install mogli

and update your environment file

config.gem "facebooker2"
config.gem "mogli"

If you use bundler, update your Gemfile

gem 'facebooker2'
gem 'mogli'

and run bundle install

You don’t need to follow the instructions on the readme pages, but if you want to have a look, check their readme here:

github.com/mmangino/facebooker2 github.com/mmangino/mogli

2. Install the Authlogic Facebook Connect plugin

$ script/plugin install git://github.com/sowenjub/authlogic_facebook_connect.git

3. Make some changes to your database

class AddFacebookConnectFieldsToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :facebook_uid, :string
    add_column :users, :facebook_access_token, :string
  end

  def self.down
    remove_column :users, :facebook_access_token
    remove_column :users, :facebook_uid
  end
end

4. Follow the instructions below (taken from the facebooker2 readme)

  1. Create config/facebooker.yml with the appropriate environment.

production:

app_id: <your application id>
secret: <your application secret>
api_key: <your application key>
  1. Create config/initializers/facebooker2.rb and place the following line in it

Facebooker2.load_facebooker_yaml

  1. Add the following line to your app/controllers/application_controller.rb

include Facebooker2::Rails::Controller

4. Make your layout look something like this

Add the buttons provided by facebooker2 using the oauth_callback route, like so (the fb_connect_async_js is only needed once per page): <%= fb_connect_async_js %> <%= fb_login_and_redirect(oauth_callback_url, :perms => User::FACEBOOK_SCOPE, :display => ‘popup’) %>

or you can do something a bit more user friendly, for people who are logged on facebook but not on your site. With the code below, they will see their name next to the button.

<%= fb_connect_async_js %> <% if current_facebook_user && current_facebook_user.fetch %> <%= fb_login_and_redirect(oauth_callback_url, :perms => User::FACEBOOK_SCOPE, :display => ‘popup’) %> <%= “ as #{current_facebook_user.first_name} #{current_facebook_user.last_name} (” + fb_logout_link(“change account?”, request.url) + “)” %> <% else %> <%= fb_login_and_redirect(oauth_callback_url, :perms => User::FACEBOOK_SCOPE, :display => ‘popup’) %> <% end %>

Notes

If you want to save some user data when connecting to facebook you can use the before_connect hook in your user model. Ex:

def before_connect(facebook_user)

# facebook_uid and facebook_access_token are automatically set by the plugin

self.first_name = facebook_user.first_name
self.last_name = facebook_user.last_name
self.gender = facebook_user.gender

# Set other tokens

  self.single_access_token = Authlogic::Random.friendly_token
  self.perishable_token = Authlogic::Random.friendly_token
  reset_persistence_token
end

If you want to access facebook data in some other controller, it’s very easy, just add these two lines

client = Mogli::Client.new(@current_user.facebook_access_token)

fb_user = Mogli::User.find(@current_user.facebook_uid, client)

And now you can use fb_user.gender to get the user gender for exemple.

For more information about what you can get form the facebook_user checkout the Mogli gem rdoc.

About

Extension of the Authlogic library to add Facebook Connect support built upon the excellent facebooker gem

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%