Michael Anderson Michael Anderson - 1 month ago 10x
Node.js Question

Can the callback for facebook-pasport be dynamically constructed?

When using facebook-passport the usual thing to do is to specify the

in the constructor of the FacebookStrategy thst you use, something like this:

passport.use("facebook", new FacebookStrategy({
//TODO: Correctly configure me
clientID: "XXXXXXX"
, clientSecret: "XXXXXXXXX"
, callbackURL: "http://localhost:3007/auth/facebook/callback"
function(accessToken,refreshToken,profile,done) {
User.findByFacebookId(profile.id, function(err,user) {
if(err){ return done(err);}
if(!user){ return done(null,false)}
return done(null, user);

Then you would set up routes like this:

app.get('/auth/facebook/login', passport.authenticate('facebook') );
app.get('/auth/facebook/login_callback', passport.authenticate('facebook', {
, failureRedirect:"/login_failed.html"

Is it possible to change the callback url so that it contains information from parameters passed to the initial login call?

NOTE: This question is more for preserving info that took me a while to work out, to avoid others going down the same paths.


I found the answer using some info found here https://github.com/jaredhanson/passport-facebook/issues/2 and through digging through the way the passport oauth2 component determines callback uris, and information about passport custom callbacks at the bottom of this page http://passportjs.org/guide/authenticate/.

Here's an example that maps calls to /auth/facebook/login/1234 to use the callback /auth/facebook/login_callback/1234

app.get('/auth/facebook/login/:id', function(req,res,next) {
     {callbackURL: '/auth/facebook/login_callback/'+req.params.id }

app.get('/auth/facebook/login_callback/:id', function(req,res,next) {
     , successRedirect:"/login_ok.html"
     , failureRedirect:"/login_failed.html"
   ) (req,res,next);