简体   繁体   English

如何在没有重定向的情况下使用Passport-Facebook登录?

[英]How to use Passport-Facebook login without redirection?

I'm building a phonegap application which will have nodejs at the server side. 我正在构建一个在服务器端有nodejs的phonegap应用程序。 I wanted to implement login using passport-facebook strategy but their callbacks specify two routes, /successcallback and /failurecallback. 我想使用passport-facebook策略实现登录,但是他们的回调指定了两个路由,/ successcallback和/ failurecallback。 Having a single page application, this makes it very confusing to have users redirected to so and so page. 拥有单个页面应用程序,这使得将用户重定向到某某页面非常困惑。

I don't want to serve static files (index.html, login.html) from the server but rather have them on the client and ask the client to make ajax calls. 我不想从服务器提供静态文件(index.html,login.html),而是在客户端上提供它们并要求客户端进行ajax调用。 So far, I'm able to make /auth/facebook call as an AJAX request but I can't receive any response on the same request because the login strategy requires the user to be redirected. 到目前为止,我能够将/ auth / facebook调用作为AJAX请求,但我无法在同一请求上收到任何响应,因为登录策略要求重定向用户。 I'd rather want to send a user_id or name back to the user on successful login or show him the login form (which is also on the www directory in phonegap) on failure. 我宁愿在成功登录时向用户发送user_id或名称,或者在失败时向他显示登录表单(也在phonegap的www目录中)。 But the redirection and CORS errors are preventing me from doing this. 但重定向和CORS错误阻止我这样做。 Is there any way I can implement this? 有什么方法可以实现吗? I've looked for this since a few weeks now, but no success. 我几周以来一直在寻找这个,但没有成功。 I'd really appreciate your help! 我真的很感谢你的帮助!

PS: I'd rather avoid having to send all html and static content from the node server. PS:我宁愿避免从节点服务器发送所有html和静态内容。

EDIT: Adding login code for better understanding: 编辑:添加登录代码以便更好地理解:

app.get('/userpage', utility.isLoggedIn, function(req, res)
{
    res.send('User:'+req.user);
});

app.get('/', utility.isLoggedIn, function(req, res)
{
    res.redirect('/userpage');
});

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

app.get('/auth/facebook/callback',passport.authenticate('facebook',
{
    successRedirect : '/',
    failureRedirect : '/login'
}));

app.get('/logout', function(req, res)
{
    req.logout();
    res.redirect('/login');
});


utility.isLoggedIn:
function isLoggedIn(req, res, next)
{
    if (req.isAuthenticated())
    return next();

    res.redirect('/login');
}

Instead of using the standard redirections offered by passport, you can define your own function which will be executed instead of the redirection. 您可以定义自己的函数而不是重定向,而不是使用护照提供的标准重定向。 Here's an example of what that code would look like 这是代码看起来像什么的一个例子

 passport.authenticate('login', function(err, user, info) {
      if (err) { return next(err); }
      if (!user) { return res.json({ status:"failed", "error": "Invalid credentials" }); }

// req / res held in closure
      req.logIn(user, function(err) {
          if (err) { return next(err); }

          return res.json({ "status":"success"});
      })
 })(req, res, next);

You can't do that with facebook oAuth, but Facebook provides another login solution where you can code your client app to request a token, that you can later validate on the server with passport-facebook-token . 你无法用facebook oAuth做到这一点,但Facebook提供了另一种登录解决方案 ,你可以在其中编写客户端应用程序来请求令牌,以后可以使用passport-facebook-token在服务器上验证。

This way you can use the advantages of passport for persistent sessions, without that annoying redirection. 通过这种方式,您可以将持久会话的优势用于持久会话,而无需令人烦恼的重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM