【问题标题】:how to resolve firebase $waitForAuth() .then do something如何解决 firebase $waitForAuth() 。然后做点什么
【发布时间】:2016-01-07 02:11:34
【问题描述】:

在我的应用程序中,我想在首页加载之前解决一些问题。在我的其他页面上,这对我来说很容易,因为我在解决方案中使用了此功能:

userAccount.$inject = ['userService','authService'];
function userAccount(userService, authService) {
  return authService.firebaseAuthObject.$requireAuth()
    .then(function(response) {
      return userService.getAccount(response.uid).$loaded();
    });
}

它需要授权,一旦返回承诺,它就会获取我保存在其他地方的 firebase 中的用户数据。如果承诺被撤销,它将不会加载该页面。

但在主页上我想检查用户是否已登录,如果用户已登录,请获取用户数据并转到页面。但是如果用户没有登录,仍然去页面只是用户数据将是空的。

我目前正在使用它来检查身份验证:

checkAuth.$inject = ['userService', 'authService'];
function checkAuth(userService, authService) {
  return authService.firebaseAuthObject.$waitForAuth()
}

这很好用,但我仍然想在加载页面之前解析用户数据,前提是用户已登录。

我以为这是我的答案:

checkAuth.$inject = ['userService','authService'];
function checkAuth(userService, authService) {
  return authService.firebaseAuthObject.$waitForAuth()
    .then(function(response) {
      return checkAuth.getAccount(response.uid).$loaded();
    });
}

这将等待承诺然后加载用户数据,但不幸的是这不起作用。

关于如何在 $waitforauth() 函数运行后检索用户数据的任何想法,关键是我想在页面加载之前解析这些数据。

谢谢大家

我的状态也不起作用:

    .state('home', {
    url: '/',
    templateUrl: '/views/main.html',
    controller: 'MainCtrl as vm',
    resolve: {
      checkAuth: checkAuth,
      userAccount: function(checkAuth){
        console.log(checkAuth);
        if(checkAuth){
          // grab user data
        } else {
          return false;
        }

      }
    }
  })

【问题讨论】:

标签: angularjs firebase angularfire


【解决方案1】:

这帮我搞定了。

状态:

  .state('main', {
    url: '/',
    templateUrl: '/views/main.html',
    controller: 'MainCtrl as vm',
    resolve: {
      userAccount: userAccountMain
    }
  })

功能:

userAccountMain.$inject = ['userService', 'authService'];
function userAccountMain(userService, authService) {
  return authService.firebaseAuthObject.$waitForAuth()
    .then(function(response){
      if(response){
        return userService.getAccount(response.uid).$loaded();
      } else {
        return false;
      }
    });
}

【讨论】:

    猜你喜欢
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2010-09-14
    • 2018-06-10
    • 1970-01-01
    • 2019-11-19
    • 2019-12-29
    相关资源
    最近更新 更多