如何在Passport-Facebook身份验证中传递额外数据



我正在尝试在使用passport-facebook身份验证策略的Node.js中编写Web应用程序。默认情况下,该示例中提供的机制可以通过将用户重定向到Facebook API来工作,用户可以将用户名和密码放在登录位置。Facebook验证用户后,它将令牌传递回我的应用程序中的回调URL。

现在这些东西适合我的应用程序。我可以毫无问题地使用Facebook登录。但是我希望当我尝试登录时将一个额外的字段传递给服务器。

基本上我的计划是在我的主页中添加TextFieldLogin Button。成功登录后,执行回调函数后,我想从此回调中捕获TextField的值。有什么办法吗?任何帮助都将受到高度赞赏。

这是我的HTML页面的主体

<body>
    <div class="container">
        <div class="jumbotron text-center">
            <p>Login or Register with:</p>
            <a href="/auth/facebook" class="btn btn-primary"><span class="fa fa-facebook"></span> Facebook</a>
        </div>
    </div>
</body>

这是我的nod.js代码:

app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook/callback', passport.authenticate('facebook', {
    successRedirect :   '/dashboard',
    failureRedirect :   '/'
}));
app.get('/dashboard', isLoggedIn, function(req, res){
    res.render('dashboard.ejs', {
        user : {
            name : req.user.facebook.name,
            profilePic : req.user.facebook.profilePic
        }
    });
});

基本上,我只想在我的LogIn按钮之前添加额外的TextField。从/dashboard路线中,我想获得该TextField的值。因此,我的/dashboard代码看起来像这样:

app.get('/dashboard', isLoggedIn, function(req, res){
    res.render('dashboard.ejs', {
        user : {
            role:   VALUE_FROM_TEXT_FIELD,
            name : req.user.facebook.name,
            profilePic : req.user.facebook.profilePic
        }
    });
});

所以我有两个问题:1.如何从前端传递此TextField值。它应该在Form内吗?2.如何捕获/dashboard路线内TextField的值?

您是否考虑过使用自定义回调代替标准Passport.js One?

"本地"策略的一个例子是:

app.get('/login', function(req, res, next) {
  passport.authenticate('local', function(err, user, info) {
    if (err) { return next(err); }
    if (!user) { return res.redirect('/login'); }
    req.logIn(user, function(err) {
      if (err) { return next(err); }
      return res.redirect('/users/' + user.username);
    });
  })(req, res, next);
});

您应该能够在请求中传递其他值并在回调中处理。

编辑:对于您的特定情况,您可能会在此方面查看某些内容...

app.get('/auth/facebook/callback', function (req, res, next) {
    passport.authenticate('facebook', function (err, user, info) {
        if (err) {
            return next(err);
            // let the next route handler do its thing...
            // or you can handle the error yourself within this
            // callback.
        }
        // if the user returns as nothing (doesn't exist) then redirect
        if (!user) {
            // this takes the place of failureRedirect
            return res.redirect('/');
        }
        req.logIn(user, function (err) {
            if (err) {
                return next(err); // again - on error, 'next' depending on your requirement.
            }
            // this takes the place of successRedirect
            return res.redirect('/dashboard');
        });
    })(req, res, next);
});

希望这澄清:)

我所做的是传递自定义函数,而不是使用supessredirect并传递变量需求。在我的情况下,是req.session.backurl将它们发送回页面上需要认证以访问的页面。

users.get('/auth/facebook/callback',
         //req.session.backURL||
  passport.authenticate('facebook', { failureRedirect: '/login' }),function(req,res){
res.redirect(req.session.backURL||'/'); //custom function used instead of sucessRedirect
});

最新更新