2017-02-13 52 views
1

我有一個AngularJS應用程序,我一直使用Firebase來保存數據。通過AngularJS應用程序進行Firebase 3.0認證

現在我一直致力於按照Firebase 3.0指南重構我的認證代碼。

我面臨的問題是,登錄後,app.js文件中定義的「分類」狀態不會加載。控制檯也不會拋出任何錯誤。

在我的應用程序中,我想讓我的用戶在第一個實例中被定向到登錄頁面。所以爲此,我在我的app.js文件中有以下代碼。我沒有重構此文件中的任何代碼。請讓我知道這是否是問題。

angular 
.module('ngClassifieds', ['ngMaterial', 'ui.router', 'firebase']) 


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

    .config(function($mdThemingProvider, $stateProvider, $urlRouterProvider) { 

    $mdThemingProvider 
     .theme('default') 
     .primaryPalette('blue-grey') 
     .accentPalette('orange') 
//  .backgroundPalette('blue-grey'); 


    $urlRouterProvider.otherwise('/auth'); 

    $stateProvider 
     .state('auth', { 
      url: '/auth', 
      templateUrl: 'components/auth/auth.tpl.html', 
      controller: 'authCtrl', 

     }) 


    $stateProvider 
     .state('classifieds', { 
      url: '/notes', 
      templateUrl: 'components/classifieds/classifieds.tpl.html', 
      controller: 'classifiedsCtrl', 
      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.ref.$requireAuth(); 
       }] 
      } 

     }) 

以下是我的auth.ctr.js文件:

(function() { 

    "use strict"; 

    angular 
    .module('ngClassifieds') 
    .controller('authCtrl', function($scope, auth, $state, $firebaseAuth) { 

     var auth = $firebaseAuth(); 


    $scope.login = function() { 

     auth 
      .$signInWithEmailAndPassword($scope.email, $scope.password) 
      .then(function(result) { 
      $state.go('classifieds'); 
      }) 
      .catch(function(error) { 
      $scope.error = error; 
      }); 
     } 

    }); 

})(); 

以下是我的auth.fac.js:

(function() { 

    "use strict"; 

    angular 
    .module('ngClassifieds') 
    .factory('auth', function($firebaseAuth) { 


     function signOut() { 
     return firebase.auth().signOut() 
      .then(function() { 
      console.log('signed out') 
      }) 
      .catch(function(error) { 
      console.log(error); 
      }); 
     } 

     return { 
     signOut: signOut 
     } 

    }); 

})(); 

而且,我的index.html有每個firebase 3.0文檔所需的信息:

<script src="https://www.gstatic.com/firebasejs/3.6.9/firebase.js"></script> 


<script> 
    // Initialize Firebase 
    var config = { 
    apiKey: "XXX", 
    authDomain: "XXX.firebaseapp.com", 
    databaseURL: "https://XXXX.firebaseio.com", 
    storageBucket: "XXX.appspot.com", 
    messagingSenderId: "XXX" 
    }; 
    firebase.initializeApp(config); 
</script> 

<script src="https://cdn.firebase.com/libs/angularfire/2.3.0/angularfire.min.js"></script> 

我很感謝在這個問題上的任何指導。

感謝

+0

請添加一個plunker或類似的東西,再現這個問題。這樣調試很困難。 –

+0

[遷移AngularJS應用程序和Firebase 2.xx到Firebase 3.xx的身份驗證問題]的可能重複(http://stackoverflow.com/questions/42137906/authentication-issue-with-migrating-angularjs-application-and-firebase -2-xx-to) –

回答

0

的問題是在第一個參數功能 「$ rootScope $上(......)。」那麼$ stateChangeError被刪除。事實上,現在所有的狀態事件現在都已被棄用和默認禁用。看到這個鏈接它可能會幫助你:https://ui-router.github.io/ng1/docs/latest/modules/ng1_state_events.html。此鏈接,你可能會在這裏找到解決方案:https://ui-router.github.io/guide/ng1/migrate-to-1_0

+0

你需要添加 添加到您的insex.html文件中 –

相關問題