使用路由器

时间:2016-12-20 16:05:05

标签: angularjs authentication firebase firebase-authentication angularfire

我试图让基本的Firebase进行身份验证以使用路由器(ui-router),如this示例所示。出于某种原因,我无法使其发挥作用。它拒绝我进入任何状态,当我尝试在运行函数中记录错误时,它会以this错误响应。

这是我的代码:

var app = angular.module('app', ['ui.router']);

app.run(["$rootScope", "$location", function($rootScope, $location) {

    $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") {
            $state.go("home");
        }
     });
}]);

app.config(function($stateProvider, $urlRouterProvider, $locationProvider) {

    $stateProvider
    .state('home', {
        url: '/',
        templateUrl: 'partials/home.html',
        controller: 'mainController',
        resolve: {
            // controller will not be loaded until $waitForSignIn resolves
            // Auth refers to our $firebaseAuth wrapper in the factory below
            "currentAuth": ["Auth", function(Auth) {
                // $waitForSignIn returns a promise so the resolve waits for it to complete
                return Auth.$waitForSignIn();
            }]
        }
    })
    .state('create', {
        url: '/create',
        templateUrl: 'partials/create.html',
        controller: 'createController',
        resolve: {
            // controller will not be loaded until $waitForSignIn resolves
            // Auth refers to our $firebaseAuth wrapper in the factory below
            "currentAuth": ["Auth", function(Auth) {
                // $waitForSignIn returns a promise so the resolve waits for it to complete
                return Auth.$waitForSignIn();
            }]
        }
    })
    .state('share', {
        url: '/share',
        templateUrl: 'partials/share.html',
        controller: 'shareController',
        resolve: {
            // controller will not be loaded until $requireSignIn resolves
            // Auth refers to our $firebaseAuth wrapper in the factory below
            "currentAuth": ["Auth", function(Auth) {
                // $requireSignIn 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.$requireSignIn();
            }]
        }
    });

    $urlRouterProvider.otherwise('/');
});

app.factory("Auth", ["$firebaseAuth",
    function($firebaseAuth) {
        return $firebaseAuth();
    }
]);

1 个答案:

答案 0 :(得分:1)

你错过了firebase依赖:

var app = angular.module('app', ['ui.router', 'firebase']);