Can the callback for facebook-pasport be dynamically constructed?

When using facebook-passport the usual thing to do is to specify the redirect_uri 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(, 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 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

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/' }

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

@OMGPOP, here you can pass in query params into your callbackUrl.

var Passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
const Router = require("express").Router();
var fbConfig = {
  display: "popup",
  clientID: "YourFbClientId",
  clientSecret: "YourFbClientSecret",
  callbackURL: "http://localhost:8686/auth/facebook/callback",
  profileFields: ['id', 'name', 'gender', 'displayName', 'photos', 'profileUrl', 'email']

Passport.use(new FacebookStrategy(fbConfig,
  function(accessToken, refreshToken, profile, callback) {
    return callback(null, accessToken);

Router.get("/auth/facebook", function(req, res, next) {
  var callbackURL = fbConfig.callbackURL + "?queryParams=" + req.query.queryParams;
  Passport.authenticate("facebook", { scope : ["email"], callbackURL: callbackURL })(req, res, next);

Router.get("/auth/facebook/callback", function(req, res, next) {
  Passport.authenticate("facebook", {
    callbackURL: fbConfig.callbackURL + "?queryParams=" + req.query.queryParams,
    failureRedirect: "/login",
    session: false
  })(req, res, next) },
  function(req, res) {
  //do whatever you want

Check out my blog for more information:

I was struggling to do this specifically with Angularjs, and wanted to redirect back to the same url that the login was initiated from.

My solution was to create a route in Angularjs that just implements a location back. I know this does not specifically answer the question, but I thought it would be helpful for anyone looking to do the same.

On the server:

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

app.get('/auth/facebook/callback', function (req, res, next) {
    var authenticator = passport.authenticate ('facebook', {
            successRedirect: '/fbcallback',
            failureRedirect: '/'

    delete req.session.returnTo;
    authenticator (req, res, next);

Angular router:

when('/fbcallback', {
    template: "",
    controller: 'fbCtrl' 

Angular controller:

app.controller("fbCtrl", function () {

You could probably do some other client side routing in the controller as well.

Need Your Help

Unexpected Caching of AJAX results in IE8 jquery ajax internet-explorer

I'm having a serious issue with Internet Explorer caching results from a JQuery Ajax request.

How to save the contents of MATLAB's Command Window to a file?

matlab file save command-window

I want to save everything in the "Command Window" to a file automatically. Is there a way to do it?