angularjs - angularfire $waitForSignIn() 用法?

标签 angularjs angularfire

我想弄清楚 $waitForSignIn()

的用法是什么

文档说:

$waitForSignIn() which returns a promise fulfilled with the current authentication state. This is useful when you want to grab the authentication state before the route is rendered

但这将如何运作?

他们说你应该实现如下(来自:link):

app.config(["$stateProvider", function ($stateProvider) {
  $stateProvider
    .state("home", {
      // the rest is the same for ui-router and ngRoute...
      controller: "HomeCtrl",
      templateUrl: "views/home.html",
      resolve: {
        // controller will not be loaded until $waitForSignIn resolves
        // Auth refers to our $firebaseAuth wrapper in the example above
        "currentAuth": ["Auth", function(Auth) {
          // $waitForSignIn returns a promise so the resolve waits for it to complete
          return Auth.$waitForSignIn();
        }]
      }
    })
    .state("account", {
      // the rest is the same for ui-router and ngRoute...
      controller: "AccountCtrl",
      templateUrl: "views/account.html",
      resolve: {
        // controller will not be loaded until $requireSignIn resolves
        // Auth refers to our $firebaseAuth wrapper in the example above
        "currentAuth": ["Auth", function(Auth) {
          // $requireSignIn returns a promise so the resolve waits for it to complete
          // If the promise is rejected, it will throw a $stateChangeError (see above)
          return Auth.$requireSignIn();
        }]
      }
    });
}]);

所以您希望在用户登录时加载 HomeCtrl 吗?但是当没有用户登录时,它会在 null 上加载 currentAuth,

然后我尝试在 currentAuth 上放置一个观察者,当用户登录时没有变化。但是没有触发登录或退出的事件

接下来我想它只会在您尝试登录时等待(例如通过弹出窗口)。所以我启动了弹出窗口,然后直接切换到主页,但随后我从 currentAuth 得到了一个 null,如果我刷新,我得到了我的用户数据。

那有什么意义呢?只是为了在用户可用时解决用户问题?

最佳答案

您应该期望您的 HomeCtrl 知道用户是否已登录,如果用户未登录,您的帐户路由将限制访问。

关于angularjs - angularfire $waitForSignIn() 用法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38170899/

相关文章:

javascript - 级联下拉列表AngularJS

javascript - Protractor 测试 - 如何将鼠标位置重置为默认原点

angularjs - 在angularjs $ http中处理重定向

javascript - Firebase:$保存到尚未创建的对象

javascript - 关闭模态不会清除文本区域数据,但会清除模型

javascript - 使用 Angular UI Router 从 URL 字符串获取状态

javascript - 使用 angularJS 更新 Firebase 中的特定记录

json - 如何在所有子项中查询具有特定值的属性的firebase

javascript - 如何将 Angular forEach 与 firebase 一起使用

javascript - 在 Firebase 中查询数据时是否还有像 ~ 这样的运算符