【问题标题】:Issues with Firebase Auth.$requireAuth()Firebase Auth.$requireAuth() 的问题
【发布时间】:2016-01-27 16:05:36
【问题描述】:

所以我正在阅读 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

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

我已经找了几个小时来寻找解决方案,大多数人说这是一个依赖问题,因为我的 app.js 文件中没有包含“ngRoute”。但是我确实包含了该依赖项,所以我知道这不是问题。

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: '/'
      });
  });

感谢您的宝贵时间。

【问题讨论】:

  • 您是否尝试过像这样声明 $routerProvider:.config( [$routerProvider , function($routeProvider) {...}]);
  • 如果你不介意我问,那会有什么不同?我试图这样设置它只是为了看看是否有任何变化,我只是得到一个错误,说 $routeProvider 没有定义。

标签: angularjs firebase


【解决方案1】:

我认为你的工厂

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 2021-10-09
    • 2019-02-10
    • 1970-01-01
    • 2020-02-26
    • 2021-06-11
    • 2018-06-06
    相关资源
    最近更新 更多