Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passport Facebook Verify Callback not Called

I'm making a node app, and when I try to use passport with Facebook, The verify callback doesn't run(nothing is logged). Any help would be appreciated.

var express = require('express');
var routes = require('./routes/routes.js');
var layout = require('./routes/layout.js');
var facebook = require('./routes/facebook.js');
var editprof = require('./routes/editprof.js');
var app = express();
var vogels = require('vogels');
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var configAuth = require('./config/auth');


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

...

passport.use(new FacebookStrategy({

    // pull in our app id and secret from our auth.js file
    clientID        : configAuth.facebookAuth.clientID,
    clientSecret    : configAuth.facebookAuth.clientSecret,
    callbackURL     : 'http://localhost:8080/auth/facebook/callback'

},

// facebook will send back the token and profile
function(token, refreshToken, profile, done) {
    console.log("TOKEN",token);
    // asynchronous
    process.nextTick(function() {
        console.log("TOKEN",token);
        console.log("ID", profile.id);
        return done(null, profile);

    });

}));

...

app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook/callback', facebook.callback);
like image 701
Steve Kreider Avatar asked Apr 18 '16 15:04

Steve Kreider


2 Answers

I've had the same problem and that was a DNS issue with the graph.facebook.com. This prevents the passport-oauth2 module behind passport-facebook to get the FB token. The verify callback is then skipped.

You can test if your problem is the same with the js snippet here:

const dns = require('dns')
dns.lookup('graph.facebook.com', console.log)

If this code returns something similar to:

{ Error: getaddrinfo ENOTFOUND graph.facebook.com
    at errnoException (dns.js:28:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:73:26)
  code: 'ENOTFOUND',
  errno: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'graph.facebook.com' }

Then your problem is indeed caused by a DNS issue.

There are multiple ways of fixing that DNS problem, but the simplest for me was to manually edit my /private/etc/hosts file on my mac:

Step 1: Get the graph.facebook.com's IP:

nslookup graph.facebook.com

Step 2: Update your /private/etc/hosts:

sudo vim /private/etc/hosts

Simply add a new line at the bottom of your file (e.g. graph.facebook.com)

Step 3: Test the dns.lookup('graph.facebook.com', console.log) again: If this is now resolved, that command should return something similar to:

null '157.240.8.18' 4

Hope this fixes your issue.

Cheers,

Nic

like image 101
Nicolas Dao Avatar answered Nov 07 '22 20:11

Nicolas Dao


I struggled for several hours with the issue. In my case, the problem was with the callbackURL property of Strategy constructor. The callback wasn't called when I tried to use a URL from other domain:

passport.use(new Strategy({
    clientID: FACEBOOK_APP_ID,
    clientSecret: FACEBOOK_APP_SECRET,
    callbackURL: 'https://my.domain.com/authorize'
} //callback
)

When I changed it to the link of the same domain it worked:

passport.use(new Strategy({
    clientID: FACEBOOK_APP_ID,
    clientSecret: FACEBOOK_APP_SECRET,
    callbackURL: '/auth/facebook/callback'
} //callback
)

Also, I've already had this url processed in express:

app.get('/auth/facebook/callback',
    passport.authenticate('facebook', {
        successRedirect: '/',
        failureRedirect: '/login'
    }));
like image 34
Vladyslav Zavalykhatko Avatar answered Nov 07 '22 19:11

Vladyslav Zavalykhatko