2015-12-17 4 views
0

Я пытаюсь перенаправить пользователей на страницу входа в систему, если они пытаются получить доступ к страницам, требующим их входа в систему. Я использую Firebase и AngularJS, следуя this guide. error explanation на сайте AngularJS указывает на то, что возникновение проблемы является несуществующим определением или дублированием, но я не могу идентифицировать ни одно из них в моем коде. Кроме того, трассировка стека ошибки не указывает, какой из моих файлов вызвал ошибку, указав только файл angular.js. Может ли кто-нибудь дать мне некоторое представление о том, что вызывает эту проблему?AngularJS - Неизвестный поставщик: AuthProvider

Примечание. Сайт работает без ошибок, и пользователи могут войти в систему и выйти из нее, если я не разрешу раздел разрешения $ routeProvider.

Вот мой app.js

angular.module('richWebApp', ['ngRoute', 'firebase', 'objectFilter']) 
.constant('fb', { 
    url: 'https://<my-firebase-app>.firebaseio.com/' //name removed for security reasons 
}) 
.run(function($rootScope, $location) { 
    $rootScope.$on("$routeChangeError", function(event, next, previous, error) { 
     if(error === "AUTH_REQUIRED") { 
      $location.path("/login"); 
     } 
    }); 
}) 
.config(function($routeProvider){ 
    $routeProvider. 
     when('/login', { 
      templateUrl: 'pages/login/login.html' 
     }). 
     when('/main', { 
      templateUrl: 'pages/main/main.html', 
      resolve: { 
       "currentAuth": ["Auth", function(Auth) { 
        return Auth.$requireAuth(); 
       }] 
      } 
     }). 
     when('/thread/:threadId', { 
      templateUrl: 'pages/thread/thread.html', 
      resolve: { 
       "currentAuth": ["Auth", function(Auth) { 
        return Auth.$requireAuth(); 
       }] 
      } 
     }). 
     otherwise({ 
      redirectTo: '/login' 
    }); 
}); 

Вот контроллер main.js

angular.module('richWebApp') 
.controller('mainPageController', function($scope, $location, userService, currentAuth, threadService, fb, $firebaseAuth, $filter){ 

    $scope.user = userService.getLoggedInUser(); 

    $scope.newThreadTitle = ''; 
    $scope.threadSubject = '' 

    $scope.createNewThread = false; 

    $scope.sortBy = 'dateAdded' 

    $scope.threads = threadService.getAllThreads(); 

    $scope.getSubjects = function(subject) { 
     return $scope.threads.subject; 
    } 

    $scope.beginAddThread = function() { 
     $scope.createNewThread = true; 
    } 

    $scope.addThread = function(){ 
     if(!$scope.newThreadTitle || !$scope.newThreadSubject){ 
      return false; 
     } 

     var date = new Date(); 

     var newThread = {  
      title: $scope.newThreadTitle, 
      subject: $scope.newThreadSubject, 
      username: $scope.user.name, 
      numComments: 0, 
      comments: [], 
      dateAdded: date.getTime() 
     }; 

     $scope.threads.$add(newThread); 

     $scope.newThread = ''; 
     $scope.newThreadTitle = ''; 
     $scope.newThreadSubject = ''; 

     $scope.createNewThread = false; 
    } 

    $scope.sortByDate = function() { 
     $scope.sortBy = 'dateAdded'; 
    } 

    $scope.sortByPopularity = function() { 
     $scope.sortBy = 'numComments'; 
    } 

    $scope.searchSubject = function(subject) { 
     $scope.searchThread = subject; 
    } 

    $scope.logout = function(){ 
     userService.logout(); 
    } 

}); 

Вот контроллер thread.js

angular.module('richWebApp') 
.controller('threadPageController', function($scope, $location, $routeParams, $filter, currentAuth, threadService, fb, userService){ 

    var threadId = $routeParams.threadId; 

    $scope.newComment = ''; 

    var thread = threadService.getThread(threadId); 

    thread.$bindTo($scope, 'thread') 

    $scope.addComment= function(){ 
     if(!$scope.newComment){ 
      return false; 
     }  

     var currentUser = userService.getLoggedInUser(); 

     var date = new Date(); 
     var newComment = { 
      text: $scope.newComment, 
      username: currentUser.name, 
      dateAdded: date.getTime(), 
      userPic: currentUser.profilePic   
     }; 

     $scope.thread.comments = $scope.thread.comments || []; 
     $scope.thread.comments.push(newComment); 
     $scope.thread.numComments += 1; 

     $scope.newComment = ''; 
    } 
}); 
+1

Вы включили скрипт службы Auth в свой index.html? –

+0

У меня есть сценарий angular-route.js, на который ссылаются, если это то, что вы имеете в виду – Briscoooe

ответ

2

Ваш код имеет в виду фабрика Auth, которая показана в примере под номером Retrieving Authentication State. Включите это в свой код.

.factory("Auth", ["$firebaseAuth", 
    function($firebaseAuth) { 
    var ref = new Firebase("<YOUR FIREBASE>"); 
    return $firebaseAuth(ref); 
    } 
]); 
+0

Моя ошибка! Я должен был прочитать всю страницу. благодаря – Briscoooe

Смежные вопросы