21

I've seen multiple tools for working with node.js and facebook connect. However many of them seem incomplete, overly-complex (non abstract) or no longer updated/maintained.

I've found these three projects:

https://github.com/DracoBlue/node-facebook-client

https://github.com/dominiek/node-facebook

https://github.com/egorFiNE/facebook-connect

https://github.com/ciaranj/node-oauth

Here one of the authors even discusses why he once again rolled his own, due to shortcomings in other implementations:

http://groups.google.com/group/nodejs/browse_thread/thread/bb46cb08e51fdda6

Does anyone have any real experience actually authenticating users and storing their facebook id's in their database using node.js and facebook connect?

I have a feeling that the answer is pretty much no and I'll have to build on top of one of the above systems to make things much simpler, but I wanted to check first.

Edit: Note make sure you use the STABLE version of node.js

Travis
  • 7,391
  • 12
  • 43
  • 52

3 Answers3

17

Did you not find ciaranj's connect-auth

const fbId = ""; #x
const fbSecret = ""; #y
const fbCallbackAddress= "http://localhost:4000/auth/facebook";
//var RedisStore = require('connect-redis');
var express= require('express');
var auth= require('connect-auth')
var app = express.createServer();
app.configure(function(){
  app.use(express.cookieDecoder());
  app.use(express.logger());
  //app.use(connect.session({ store: new RedisStore({ maxAge: 10080000 }) }));
  app.use(express.session());
  app.use(auth( [
    auth.Facebook({appId : fbId, appSecret: fbSecret, scope: "email", callback: fbCallbackAddress})
  ]) );
});


app.get('/logout', function(req, res, params) {
    req.logout();
    res.writeHead(303, { 'Location': "/" });
    res.end('');
});

app.get('/', function(req, res, params) {
    if( !req.isAuthenticated() ) {
        res.send('<html>                                              \n\
          <head>                                             \n\
            <title>connect Auth -- Not Authenticated</title> \n\
            <script src="http://static.ak.fbcdn.net/connect/en_US/core.js"></script> \n\
          </head><body>                                             \n\
            <div id="wrapper">                               \n\
              <h1>Not authenticated</h1>                     \n\
              <div class="fb_button" id="fb-login" style="float:left; background-position: left -188px">          \n\
                <a href="/auth/facebook" class="fb_button_medium">        \n\
                  <span id="fb_login_text" class="fb_button_text"> \n\
                    Connect with Facebook                    \n\
                  </span>                                    \n\
                </a>                                         \n\
              </div></body></html>');
    } else {
         res.send( JSON.stringify( req.getAuthDetails()) );
    }
});

// Method to handle a sign-in with a specified method type, and a url to go back to ...
app.get('/auth/facebook', function(req,res) {
  req.authenticate(['facebook'], function(error, authenticated) { 
     if(authenticated ) {
        res.send("<html><h1>Hello Facebook user:" + JSON.stringify( req.getAuthDetails() ) + ".</h1></html>")
      }
      else {
        res.send("<html><h1>Facebook authentication failed :( </h1></html>")
      }
   });
});

app.listen(4000);

facebook settings

Alfred
  • 60,935
  • 33
  • 147
  • 186
  • It seems to work a lot of the way but down just hangs on facebooks redirect to /auth/facebook ... and does nothing – Travis Dec 20 '10 at 16:06
  • 1
    it works like a charm for me! You have to set const fbCallbackAddress= "http://localhost:4000/auth/facebook"; offcourse change localhost:4000/ part – Alfred Dec 20 '10 at 17:31
  • changed snippet a little bit. You have to offcourse specify auth middleware variables. – Alfred Dec 20 '10 at 17:34
  • awesome Alred thanks for your help ... turns out now it always gets rejected ... aka authenticated is always false after it goes through the process but everything else works – Travis Dec 20 '10 at 18:57
  • Alfred could i maybe chat with you directly? – Travis Dec 20 '10 at 19:18
  • 1
    Turns out anyone that finds this heres whats going on: It works fine in node v 0.2.6 but if you move up to the 0.3.7 branch it goes into an infinite loop. Check out the convo here: https://github.com/ciaranj/connect-auth/issues#issue/31 – Travis Feb 04 '11 at 06:10
  • Hm.. In my case, req.isAuthenticated() and req.authenticate() is not working. I saw this error message, "500 TypeError: Object # has no method 'authenticate'". I'm using express 2.3.10. What should I do to use those functions? – Whiteship Jun 22 '11 at 00:15
  • The last time I checked(with node v0.2.6 ;)) it was a long time ago. I would now recommend to use everyauth. I have uploaded gist at https://gist.github.com/1039402. The dependencies are already in node_modules folder, but you need to configure FacebookId,FacebookSecret,FacebookSiteURL. – Alfred Jun 22 '11 at 02:36
8

I find passport-facebook fairly simple and useful.
I also like that the core passport module has 80+ auth strategies.
(e.g. twitter, google, foursquare, github, digg, dropbox).

From the creator's github README:

// Set up the strategy
passport.use(new FacebookStrategy({
        clientID: FACEBOOK_APP_ID,
        clientSecret: FACEBOOK_APP_SECRET,
        callbackURL: "http://localhost:3000/auth/facebook/callback"
    },
    function(accessToken, refreshToken, profile, done) {
        User.findOrCreate({ facebookId: profile.id }, function (err, user) {
            return done(err, user);
        });
    }
));

// Use the authentication
app.get('/auth/facebook',
    passport.authenticate('facebook'),
    function(req, res){
        // The request will be redirected to Facebook for authentication, so
        // this function will not be called.
});

app.get('/auth/facebook/callback',
    passport.authenticate('facebook', { failureRedirect: '/login' }),
    function(req, res) {
        // Successful authentication, redirect home.
        res.redirect('/');
});
treejanitor
  • 1,249
  • 14
  • 17
1

I have used Brian Noguchi's everyauth. It works w/ node.js v.0.4.x. You can find that here.

It has native support for mongodb using mongoose-auth plugin, again written by brian.

alexandru.topliceanu
  • 2,364
  • 2
  • 27
  • 38