本文介绍了对Facebook-pasport的回调可以动态构建吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用Facebook护照时,通常要做的是在您使用的FacebookStrategy的构造函数中指定 redirect_uri ,如下所示:

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.

推荐答案

我使用这里找到的一些信息,并通过挖掘护照oauth2组件决定的方式回调uris以及有关本页面底部护照自定义回调的信息,请。

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/.

这是一个将调用映射到 / auth / facebook / login / 1234 使用回调 / auth / facebook / login_callback / 1234

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);
 });

这篇关于对Facebook-pasport的回调可以动态构建吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:31