【问题标题】:How do you show that the user is authenticated after they authenticate with Facebook?您如何证明用户在通过 Facebook 进行身份验证后已通过身份验证?
【发布时间】:2015-11-08 20:20:37
【问题描述】:

我正在关注this 教程,使用 Node 和 Passport 实现 Facebook 身份验证。

教程和docs 都说要这样做:

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

但是用户如何知道他们是否通过了身份验证?我想将数据发送到我的前端,以显示用户是否已登录,以及我可以用来更新的数据,比如我的导航栏。

据我了解,使用 Facebook 进行身份验证的过程如下:

  1. 发送GET /auth/facebook请求。
  2. 命中路线。
  3. 304 Redirect 将 URL 发送回 Facebook 门户。
  4. 使用回调 url 向 Facebook 门户发送请求。
  5. 用户在门户上点击“接受”。
  6. Facebook 发回一个304 Redirect,其中的 url 是您的回调 url。
  7. 请求被发送到您的回调 url,在本例中为 GET /auth/facebook/callback
  8. passport.use(obj, cb)cb 开始运行。
  9. 发回一个304 Redirect,根据成功或失败,URL 为//login
  10. //login 发送请求。

我不确定如何使用已登录的用户更新我的前端。

router.get('/auth/facebook/callback',
  passport.authenticate('facebook'),
  function(req, res) {
    var userCopy = JSON.parse(JSON.stringify(req.user));
    delete userCopy.auth;
    res.status(200).json(userCopy);
  }
);

这不起作用,因为没有接收数据的$http.get().then()


我尝试添加一些查询参数:

successRedirect: '/?foo=bar'

但这对我不起作用。网址栏显示http://localhost:3000/?foo=bar#_=_,这并没有记录任何内容:

app.get('/*', function(req, res) {
  var url = path.resolve(__dirname + '/../client/' + envFolder + '/index.html');
  res.sendFile(url, null, function(err) {
    if (err) {
      return res.status(500).send(err);
    }
    console.log('here');
    console.log(req.query);
    return res.status(200).end();
  });
});

当我做successRedirect: '/home?facebook=true' 时它工作。但是在sendFile 内部,我无法发送任何else 内容。如果我尝试res.send('Authenticated with Facebook'),它会给我这个错误:Error: Can't set headers after they are sent.。大概是因为sendFile 正在发回文件并设置一些标题,并且 then 我尝试用不同的标题发送回一些 else 的东西。所以我不知道该怎么办。


我得出的方法是使用run 块:

function run($http, Session, $cookies) {
  $http
    .get('/current-user')
    .then(function(response) {
      Session.setUser(response.data);
      $cookies.put('userId', response.data._id);
    })
  ;
}
  1. 我不确定这是不是最好的方法。
  2. 它弄乱了我的测试,因为他们不期待 HTTP 请求,并且当我不总是想要这样做时,它会设置 Session 和 cookie。

【问题讨论】:

    标签: javascript node.js passport.js passport-facebook


    【解决方案1】:

    您可以在视图中呈现用户对象,因此您可以通过 Angular 检查变量的存在来获取它:

    router.get('/auth/facebook/callback',
      passport.authenticate('facebook'),
      function(req, res) {
        var userCopy = JSON.parse(JSON.stringify(req.user));
        delete userCopy.auth;
        res.status(200).render('index', {user: userCopy});
      }
    );
    

    您的 HTML 索引文件应该有:

    <!--Embedding The User Object-->
    <script type="text/javascript">
        var user = <%= user | json | safe %>;
    </script>
    

    最后你的 AngularJS 应用应该有这样的服务:

    // Authentication service for user variables
    angular.module('users').factory('Authentication', [
    
        function() {
            var _this = this;
    
            _this._data = {
                user: window.user
            };
    
            return _this._data;
        }
    ]);
    

    在您的主控制器中注入身份验证服务并使用用户对象:

    angular.module('core').controller('MainController', ['$scope', 'Authentication',
        function ($scope, Authentication) {
            $scope.authentication = Authentication;
        }
    ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 2012-07-15
      • 2017-09-18
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      相关资源
      最近更新 更多