node-facebook

by dominiek

dominiek / node-facebook

Simple Facebook Integration for NodeJS (and Express)

127 Stars 16 Forks Last release: Not found 15 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

h1. Facebook Connect for NodeJS

Note: I wrote an "article on howtonode.org":http://howtonode.org/facebook-connect on how to use Facebook Connect with Node

This library contains NodeJS and jQuery scripts together with examples that allow you to easily integrate with Facebook. By making use of "Facebook's Javascript Client Library":http://wiki.developers.facebook.com/index.php/JavaScriptClientLibrary we only have to do the minimal amount effort on the server-side, which "could be a good thing":http://synaptify.com/?p=613702.

There are two main ways of integrating with Facebook and both require you to "sign up for a Facebook Application":http://facebook.com/developer. If you want your web application to run inside Facebook, you need to make sure to verify users inside the Facebook Canvas (See example 2), otherwise, if you want users to use Facebook Connect to log onto your site, check Example 1.

h2. Dependencies

In order to use these plugins, you need to install both "NodeJS":http://nodejs.org and the "Express Web Framework":http://github.com/visionmedia/express/ on top of it.

Express can easily be installed like this:

cd myproject
git init
mkdir -p lib/support

git submodule add git://github.com/visionmedia/express.git lib/support/express
cd lib/support/express
git submodule init
git submodule update

Also, we need to install "hashlib":http://github.com/brainfucker/hashlib, a NodeJS library provided by mr Brainfucker:

git submodule add git://github.com/brainfucker/hashlib.git lib/support/hashlib
cd lib/support/hashlib
make

h2. Setting up your Project

  1. Create the necessary folders:
cd myproject
mkdir -p lib
mkdir -p public/javascripts
  1. Copy "lib/facebook.js":node-facebook/blob/master/lib/facebook.js into /lib and "lib/jquery.facebook.js":node-facebook/blob/master/lib/jquery.facebook.js into public/javascripts

  2. Place a file called xdreceiver.htm into your public directory, this is for "Facebook cross-domain communication":http://wiki.developers.facebook.com/index.php/CrossDomainCommunicationChannel:

 


    Cross-Domain Receiver Page 


     


h2. Example 1: FB Connect

See "examples/fbconnect":node-facebook/blob/master/examples/fbconnect for the these example files.

h3. /app.js

require.paths.unshift(__dirname + '/lib')
require.paths.unshift(__dirname + '/lib/support/express/lib')
require.paths.unshift(__dirname + '/lib/support/hashlib/build/default')

require('express')
require('express/plugins')

configure(function(){
  use(MethodOverride)
  use(ContentLength)
  use(Cookie)
  use(Session)
  use(Logger)
  use(require('facebook').Facebook, {
    apiKey: 'e1249f7d4bc25b8f90e5c9c7523e3ee1', 
    apiSecret: '4ae45734dd66fa85c7b189fc2d7d5b4c'
  })
  set('root', __dirname)
})

// Called to get information about the current authenticated user
get('/fbSession', function(){
  var fbSession = this.fbSession()

  if(fbSession) {
    // Here would be a nice place to lookup userId in the database
    // and supply some additional information for the client to use
  }

  // The client will only assume authentication was OK if userId exists
  this.contentType('json')
  this.halt(200, JSON.stringify(fbSession || {}))
})

// Called after a successful FB Connect
post('/fbSession', function() {
  var fbSession = this.fbSession() // Will return null if verification was unsuccesful

  if(fbSession) {
    // Now that we have a Facebook Session, we might want to store this new user in the db
    // Also, in this.params there is additional information about the user (name, pic, first_name, etc)
    // Note of warning: unlike fbSession, this additional information has not been verified
    fbSession.first_name = this.params.post['first_name']
  }

  this.contentType('json')
  this.halt(200, JSON.stringify(fbSession || {}))
})

// Called on Facebook logout
post('/fbLogout', function() {
  this.fbLogout();
  this.halt(200, JSON.stringify({}))
})

// Static files in ./public
get('/', function(file){ this.sendfile(__dirname + '/public/index.html') })
get('/xd_receiver.htm', function(file){ this.sendfile(__dirname + '/public/xd_receiver.htm') })
get('/javascripts/jquery.facebook.js', function(file){ this.sendfile(__dirname + '/public/javascripts/jquery.facebook.js') })

run()

h3. /public/index.html

 


    
    
    



    

h2. Example 2: A Facebook Application running in the Canvas

See "examples/fbiframe":node-facebook/blob/master/examples/fbiframe for the these example files.

h3. /app.js

require.paths.unshift(__dirname + '/lib')
require.paths.unshift(__dirname + '/lib/support/express/lib')
require.paths.unshift(__dirname + '/lib/support/hashlib/build/default')

require('express')
require('express/plugins')

configure(function(){
  use(MethodOverride)
  use(ContentLength)
  use(Cookie)
  use(Session)
  use(Logger)
  use(require('facebook').Facebook, {
    apiKey: 'e1249f7d4bc25b8f90e5c9c7523e3ee1', 
    apiSecret: '4ae45734dd66fa85c7b189fc2d7d5b4c'
  })
  set('root', __dirname)
})

// This is the canvas URL set in the Facebook Application settings
get('/iframe', function (){
  var fbSession = this.fbSession() // Will create a session based on verified data from the GET params
  this.sendfile(__dirname + '/public/iframe.html')
})

// Called to get information about the current authenticated user
get('/fbSession', function(){
  var fbSession = this.fbSession()

  if(fbSession) {
    // Here would be a nice place to lookup userId in the database
    // and supply some additional information for the client to use
  }

  // The client will only assume authentication was OK if userId exists
  this.contentType('json')
  this.halt(200, JSON.stringify(fbSession || {}))
})

// Static files in ./public
get('/xd_receiver.htm', function(file){ this.sendfile(__dirname + '/public/xd_receiver.htm') })
get('/javascripts/jquery.facebook.js', function(file){ this.sendfile(__dirname + '/public/javascripts/jquery.facebook.js') })

run()

h3. /public/iframe.html




    Nodogoshi
    
    

    



    
Hi there !
Viewing anonymously, click here to authenticate.

h2. License

"MIT":http://www.opensource.org/licenses/mit-license.php

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.