Firebase Auth。$ requireAuth()的问题

时间:2016-01-27 16:05:37

标签: angularjs firebase

所以我正在阅读Firebase"用户身份验证"部分找到一种方法来阻止未经过身份验证的用户访问特定页面(即他们未登录时)

我遇到的解决方案是使用这段代码:

resolve: {
      // controller will not be loaded until $requireAuth resolves
      // Auth refers to our $firebaseAuth wrapper in the example above
      'currentAuth': ['Auth', function(Auth) {
        // $requireAuth 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.$requireAuth();
      }]
    }

因此,在我将其放入并尝试访问特定页面而未登录时,我在控制台中收到以下错误消息:

Error: [$injector:unpr] Unknown provider: AuthProvider <- Auth <- currentAuth

如果我尝试在单词后登录,则无法导航到该特定页面。

我一直在寻找解决方案几个小时,而且大多数人都认为这是一个依赖性问题,没有&#34; ngRoute&#34;包含在我的app.js文件中。但是我确实有这种依赖性,所以我知道这不是问题。

angular
  .module('App', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ui.sortable',
    'firebase',
    'angular-toArrayFilter'
  ])
  .constant('fb', {
    url: 'https://<url>.firebaseio.com/'
  })
  .factory('fireBaseRef', function(fb) {
    return new Firebase(fb.url);
  })
  .factory('UserAuth', function($firebaseAuth, fireBaseRef){
    return $firebaseAuth(fireBaseRef);
  })
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/login.html',
        controller: 'LoginCtrl',
        controllerAs: 'login_controller'
      })
      .when('/view_resources', {
        templateUrl: 'views/view_resources.html',
        controller: 'ViewResourcesCtrl',
        controllerAs: 'view_resources',
        resolve: {
          // controller will not be loaded until $requireAuth resolves
          // Auth refers to our $firebaseAuth wrapper in the example above
          'currentAuth': ['Auth', function(Auth) {
            // $requireAuth 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.$requireAuth();
          }]
        }
      })
      .otherwise({
        redirectTo: '/'
      });
  });

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

我认为你的工厂

.factory('UserAuth', function($firebaseAuth, fireBaseRef){
    return $firebaseAuth(fireBaseRef);
  })

应该命名为“Auth”,如下所示:

.factory('Auth', function($firebaseAuth, fireBaseRef){
    return $firebaseAuth(fireBaseRef);
  })