Node.js Passportjs Facebook登录流(passport Facebook vs passport token)

Node.js Passportjs Facebook登录流(passport Facebook vs passport token),node.js,facebook,passport.js,Node.js,Facebook,Passport.js,使用节点、Express和Passport 好的,我的团队和我正在为双面市场类型的应用程序构建一个RESTAPI。我们已经为电子邮件和密码登录设置了基本的LocalStrategy 我们希望API用户代理不可知,因此我们可以通过web、Android或iOS使用API。 但我们感到困惑的是FB登录流。问题是,Passportjs的幕后到底发生了什么。我们已经研究了“passport facebook”和“passport facebook token”策略,但无法真正决定采用哪种策略 这是我目前

使用节点、Express和Passport

好的,我的团队和我正在为双面市场类型的应用程序构建一个RESTAPI。我们已经为电子邮件和密码登录设置了基本的LocalStrategy

我们希望API用户代理不可知,因此我们可以通过web、Android或iOS使用API。

但我们感到困惑的是FB登录流。问题是,Passportjs的幕后到底发生了什么。我们已经研究了“passport facebook”和“passport facebook token”策略,但无法真正决定采用哪种策略

这是我目前对流程的理解:

如果这是正确的,我最好让客户端从FB获取访问令牌,然后将其发送给我,还是让FB通过重定向和回调URL处理它

护照令牌:

passport.use('facebook-token', new FacebookTokenStrategy( {
    clientID: 'xxx',
    clientSecret: 'xxx'
}, function(accessToken, refreshToken, profile, done) {
    // asynchronous
    //console.log("into passport auth");
    process.nextTick(function() {
        User.findOne({'facebook.id': profile.id}, function(error, user) {
            console.log("user is " + JSON.stringify(user));
            console.log("profile is " + JSON.stringify(profile));

            //do user creation stuff etc.

            return done(error, user);
        });
    });
}));

authRouter.post('/facebook', passport.authenticate('facebook-token'), function (req, res) {
    console.log("into controller");
    if (req.user){
        //log the user in since they successfully authenticated with facebook.
        req.login(user);
        res.status(200).end();
    } else {
        res.status(401).end();
    }
});
Passport facebook:

passport.use('facebook', new FacebookStrategy( {
    callbackURL: "http://75.128.65.176:8080/auth/facebook/callback",
    clientID: 'xxx',
    clientSecret: 'xxx'
}, function(accessToken, refreshToken, profile, done) {
    // asynchronous
    //console.log("into passport auth");
    process.nextTick(function() {
        User.findOne({'facebook.id': profile.id}, function(error, user) {
            console.log("user is " + JSON.stringify(user));
            console.log("profile is " + JSON.stringify(profile));

            //do user creation stuff etc.

            return done(error, user);
        });
    });
}));

// Redirect the user to Facebook for authentication.  When complete,
// Facebook will redirect the user back to the application at
//     /auth/facebook/callback
authRouter.get('/facebook', passport.authenticate('facebook'));

// Facebook will redirect the user to this URL after approval.  Finish the
// authentication process by attempting to obtain an access token.  If
// access was granted, the user will be logged in.  Otherwise,
// authentication has failed.
authRouter.get('/facebook/callback',
    passport.authenticate('facebook', { successRedirect: '/',
                                  failureRedirect: '/login' }));

非常感谢您提供有关此流程实际工作方式的任何详细信息

客户端Facebook重定向在使用本机iOS和Android Facebook SDK时很不方便,因为它们有时会重定向到用户安装的Facebook应用程序。因此,如果您想要一个真正通用的API,那么您应该使用
passport-facebook-token

验证所示的2个流量是否正确

是的,他们是正确的


问:我有一个API列表。我如何使用passport facebook策略保护他们

您有几个选项:

1.验证
Facebook令牌
  • 服务器返回
    Facebook令牌
    以及用户信息
  • 客户端每次调用API时都会发送Facebook令牌
  • 服务器验证Facebook令牌
有关如何验证Facebook令牌的更多信息

2.使用JSON Web令牌(JWT)
  • 服务器在检索Facebook用户信息后返回一个
    JWT
  • 客户端每次调用API时都会发送JWT
  • 服务器验证
    JWT
这样,服务器就不必向Facebook发送验证
Facebook令牌的请求。更多信息


问:如果我使用的是passport facebook代币,我如何告诉用户去登录facebook

您的
/api/auth/facebook
仅接受
facebook令牌
,并返回相应的
HTTP代码
。因此,要求用户登录Facebook是客户的工作


有关如何手动创建Facebook登录的更多信息。

我发现这些示例非常有用(服务器)和(客户端)