Facebook Connect with NodeJS

de-bugged picture de-bugged · Aug 8, 2013 · Viewed 10.3k times · Source

I am creating a web app with express.js and angular,js, where the client authenticates via facebook. For facebook authentication I use facebook-node-sdk to connect with facebook and retrieve details.

The code works well when authentication is made via a url redirect. But the same code returns no result when I send a POST request from client to invoke the same code

Server Code

app.post('/login', Facebook.loginRequired(), function(req, res){
  var result = {};
  req.facebook.api('/me', function(err, user) {
      console.log(user);
      if(user !== undefined){
        result.status = 1;
        result.name = user.name;
        res.contentType('json');
        res.write(JSON.stringify(result));
        res.render('index')
        res.end();
      }
      else{
        result.status = 0;
        result.name = "Error signing into Facebook";
        res.contentType('json');
        res.write(JSON.stringify(result));
        res.render('/')
        res.end();
      }
    });
});

I have no error when the above code is invoked. console.log shows:

POST /login 302.

So is it possible to authenticate facebook via POST request or should it be always a direct URL request? If so any option to authenticate via POST?

Answer

Sangram Singh picture Sangram Singh · Aug 8, 2013

If you've only just started and are going to use other Oauth authentications like gmail or twitter. I recommend using passport.js.

It is much simpler. http://passportjs.org/