Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(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', {
    successRedirect:"/login_ok.html"
  , 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.

like image 622
Michael Anderson Avatar asked Mar 20 '13 01:03

Michael Anderson


1 Answers

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) {
  passport.authenticate(
    'facebook', 
     {callbackURL: '/auth/facebook/login_callback/'+req.params.id }
  )(req,res,next);
});

app.get('/auth/facebook/login_callback/:id', function(req,res,next) {
  passport.authenticate(
    'facebook',
     {
       callbackURL:"/auth/facebook/login_callback/"+req.params.id
     , successRedirect:"/login_ok.html"
     , failureRedirect:"/login_failed.html"
     }
   ) (req,res,next);
 });
like image 133
Michael Anderson Avatar answered Oct 13 '22 22:10

Michael Anderson