2017-02-13 43 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文件中 –

相关问题