Search code examples
node.jsexpresspassport-facebook

Passport facebook authentication does not show the facebook authentication screen


When I call passport.authenticate('facebook'); , the page keeps on loading and fails with timeout. The facebook authentication screen never show up. Any clue on where I have made a mistake ?

Following are my code snippets.

       var User = require('../models/user.js'),
       passport = require('passport'),
       FacebookStrategy = require('passport-facebook').Strategy;
   passport.use(new FacebookStrategy({
           clientID: 'appId-XXX',
           clientSecret: 'appSecret-xxx',
           callbackURL: 'https://xxx/auth/facebook/callback'
       },
       function(accessToken, refreshToken, profile, done) {
           process.nextTick(function() {
               var authID = 'facebook' + profile.id;
               User.findOne({
                   authId: authID
               }, function(err, user) {
                   if (err) return done(err, null);
                   if (user) return done(null, user);
                   user = new User({
                       authID: authID,
                       name: profile.displayName,
                       created: Date.now(),
                       role: 'customer'
                   });
                   user.save(function(err) {
                       if (err) return done(err, null);
                       done(null, user);
                   });
               });
           });

       }));

   app.use(passport.initialize());
   app.use(passport.session());

   app.get('/auth/facebook', function(req, res, next) {
       console.log('Calling Facebook Authenticate');
       passport.authenticate('facebook');
   });

   app.get('/auth/facebok/callback', passport.authenticate('facebook', {
       successRedirect: options.successRedirect,
       failureRedirect: options.failureRedirect
   }));

Solution

  • passport.authenticate returns a middlware, to be used as such:

    app.get('/auth/facebook', passport.authenticate('facebook'));
    

    Since you're calling it instead of passing it directly you ought to call the middleware it returns passing it the req, res, next.

    app.get('/auth/facebook', function(req, res, next) {
        console.log('Calling Facebook Authenticate');
        passport.authenticate('facebook')(req, res, next);
    });