Ian Warburton Ian Warburton - 1 month ago 22
Node.js Question

passport-facebook not doing a redirect

Here's my silly nodejs module. When

passport.authenticate('facebook')
is called it just freezes.

I'm using Express 4.2.0. Any ideas?

The authenticate call doesn't block. How am I suppose to return from the function?

All the examples I've seen call
authenticate
in app.js. I don't want to configure authentication strategies in app.js.

var express = require('express');
var router = express.Router();

var passport = require('passport')
var facebookStrategy = require('passport-facebook').Strategy;

passport.use(new facebookStrategy({
clientID: "xxx",
clientSecret: "xxx",
callbackURL: "http://localhost:3000/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {

}
));

router.get('/', function(req, res) {
passport.authenticate('facebook');
});


module.exports = router;

Answer

You must call done() in your facebookStrategy callback function. Without that, things hang because the calling of done() is how the system knows to proceed. Even if you just have stub code in there for now, you need at least done() to keep the control flow proceeding properly.

Secondly, passport.authenticate returns a middleware function, so you don't directly call it, you pass it on to express:

router.get('/', passport.authenticate('facebook')); //don't wrap here

The second part incorrect about your above router.get callback is it never responds, which is also a way to make express hang.

Once those are fixed, you'll need one more route to handle the facebook callback:

var options = {
  successRedirect: '/home',
  failureRedirect: '/bummer-dude'
};
router.get('/auth/facebook/callback', passport.authenticate('facebook', options));