Javascript 持久Firebase OAuth身份验证

Javascript 持久Firebase OAuth身份验证,javascript,angularjs,firebase,angularfire,firebase-security,Javascript,Angularjs,Firebase,Angularfire,Firebase Security,我正在尝试在多个页面上保持Firebase用户身份验证状态 $scope.loginGoogle = function() { console.log("Got into google login"); ref.authWithOAuthPopup("google", function(error, authData) { $scope.loggedIn = true; $scope.uniqueid = au

我正在尝试在多个页面上保持Firebase用户身份验证状态

    $scope.loginGoogle = function() {
        console.log("Got into google login");
        ref.authWithOAuthPopup("google", function(error, authData) { 
           $scope.loggedIn = true;
           $scope.uniqueid = authData.google.displayName;
                 }, {
           remember: "sessionOnly",
           scope: "email"
        });
    };


        function checkLogin() {
           ref.onAuth(function(authData) {
             if (authData) {
                // user authenticated with Firebase
                console.log("User ID: " + authData.uid + ", Provider: " + authData.provider);
             } else {
               console.log("Nope, user is not logged in.");
             }
           });
        };

但是,当在另一个页面中调用checkLogin函数时,即使用户已登录到登录页面,也不会定义authData。怎么回事?

这里有两件事要知道

首先,您将JS客户端auth方法与AngularFire结合使用。虽然这不是一件坏事,但您需要注意一些问题

其次,您可以使用
$firebaseAuth
不处理下面所有的疯狂内容

当使用Firebase JS客户端级别的函数时,Angular由于其摘要循环而不会总是使用它们。对于任何外部JS库都是如此。解决这个问题的方法是使用
$timeout
服务

// inject the $timeout service
app.controller("fluttrCtrl", function($scope, $firebase, $timeout) {

  var url = "https://crowdfluttr.firebaseio.com/";
  var ref = new Firebase(url);
  $scope.loginGoogle = function() {
    console.log("Got into google login");

    ref.authWithOAuthPopup("google", function(error, authData) {

    // wrap this in a timeout to allow angular to display it on the next digest loop
    $timeout(function() {
      $scope.loggedIn = true;
      $scope.uniqueid = authData.google.displayName;
    });

    }, {
      remember: "sessionOnly",
      scope: "email"
    });

  });

});
通过将
$scope
属性包装在
$timeout
中,将运行另一个循环,并在页面上显示


理想情况下,你不想自己处理这个问题。使用AngularFire内置的
$firebaseAuth
模块。您需要升级到0.9版本才能使用该模块。

这里有两件事需要知道

首先,您将JS客户端auth方法与AngularFire结合使用。虽然这不是一件坏事,但您需要注意一些问题

其次,您可以使用
$firebaseAuth
不处理下面所有的疯狂内容

当使用Firebase JS客户端级别的函数时,Angular由于其摘要循环而不会总是使用它们。对于任何外部JS库都是如此。解决这个问题的方法是使用
$timeout
服务

// inject the $timeout service
app.controller("fluttrCtrl", function($scope, $firebase, $timeout) {

  var url = "https://crowdfluttr.firebaseio.com/";
  var ref = new Firebase(url);
  $scope.loginGoogle = function() {
    console.log("Got into google login");

    ref.authWithOAuthPopup("google", function(error, authData) {

    // wrap this in a timeout to allow angular to display it on the next digest loop
    $timeout(function() {
      $scope.loggedIn = true;
      $scope.uniqueid = authData.google.displayName;
    });

    }, {
      remember: "sessionOnly",
      scope: "email"
    });

  });

});
通过将
$scope
属性包装在
$timeout
中,将运行另一个循环,并在页面上显示


理想情况下,你不想自己处理这个问题。使用AngularFire内置的
$firebaseAuth
模块。您需要升级到0.9版本才能使用该模块。

onAuth
中,您在哪里定义
ref
变量。您能提供一个JSBin或Plnker来显示您的问题吗?我们这里有一个JSFIDLE:您的示例没有正确加载。它正在控制台中抛出错误。下面是一个代码笔:您在
onAuth
中定义
ref
变量的位置。您能提供一个JSBin或Plnker来显示您的问题吗?我们这里有一个JSFIDLE:您的示例没有正确加载。它正在控制台中抛出错误。这是一个代码笔: