2016-10-03 44 views
1

我想使用Firebase V3,AngularJS和ui.router保護我的網站上的各種路線。如何使用Firebase v3保護Angular路線?

This看起來像一個類似的問題。我遵循了SO帖子的步驟,但它不適合我。

我希望發生什麼: 當點擊FAQ鏈接,如果註銷,登錄時應顯示的FAQ頁面,我應該被轉發到登錄頁面

居然會發生什麼: FAQ頁面根本無法訪問。登錄沒有任何區別。它也不會在註銷時將我轉到登錄頁面。

我在我的run函數中收到此錯誤。

ReferenceError: Firebase is not defined(…)  

我已經包含AngularFire頁面上,如果這樣,我沒有得到,即使我從依賴關係數組中刪除火力地堡模塊注入錯誤。

var app = angular.module('app', ['ui.router', 'firebase']); 
app.constant('FirebaseDatabaseUrl', 'https://myfbdb.firebaseio.com'); 
app.config(function($stateProvider, $urlRouterProvider, $firebaseRefProvider, FirebaseDatabaseUrl) { 
    $firebaseRefProvider.registerUrl(FirebaseDatabaseUrl); 
// If a route other than status is requested, 
// go to the auth route 
//$urlRouterProvider.otherwise('/logintest/login'); 

$stateProvider 
    .state('login', { 
     url: '/login', 
     templateUrl: 'pages/login.html', 
     controller: 'LoginController as login' 
    }) 

    .state('faq', { 
     url: '/faq', 
     templateUrl: 'pages/faq.html', 
     controller: 'FaqController as faq', 
     resolve: { 
      // controller will not be loaded until $requireSignIn resolves 
      "firebaseUser": ["$firebaseAuthService", function($firebaseAuthService) { 
      console.log('waitForSignIn') 
        // $waitForSignIn returns a promise so the resolve waits for it to complete 
        return $firebaseAuthService.$waitForSignIn(); 
      }] 
      } 

    }) 
    .state('about', { 
     url: '/about', 
     templateUrl: 'pages/about.html', 
     controller: 'AboutController as about', 
     resolve: { 
      // controller will not be loaded until $requireSignIn resolves 
      "firebaseUser": ["$firebaseAuthService", function($firebaseAuthService) { 
      // If the promise is rejected, it will throw a $stateChangeError 
      return $firebaseAuthService.$requireSignIn(); 
      }] 
     }   
    }) 

}); 



app.controller('FaqController', ['$scope', 'firebaseUser', function($scope, firebaseUser){ 
console.log('faq') 
}]); 





app.run(["$rootScope", "$state", function($rootScope, $state) { 
    console.log('run'); 
    $rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) { 
// We can catch the error thrown when the $requireSignIn promise is rejected 
// and redirect the user back to the home page 
if (error === "AUTH_REQUIRED") { 
    console.log('redirecting to login page') 
    $state.go("login"); 
} 
}); 
    }]); 
+0

它看起來像你使用AngulsrFire舊版本。你的版本號是多少? –

+0

嘿,謝謝你的回覆。我正在使用v1.2.0。 – user1532669

+0

這將是問題。升級到2.0+ –

回答

2

AngularFire 2.0+版本與Firebase 3.0兼容。 AngularFire 2.0以下的任何版本都適用於舊版Firebase。