Firebase 使用路由器进行身份验证
Firebase authenticating with routers
我正在尝试让基本的 Firebase 身份验证与路由器(ui-路由器)一起工作,如 this example. For some reason I can't get this to work. It rejects me from going to any of the states and when I try to log the error in the run function it responses with 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();
}
]);
您缺少 firebase 依赖项:
var app = angular.module('app', ['ui.router', 'firebase']);
我正在尝试让基本的 Firebase 身份验证与路由器(ui-路由器)一起工作,如 this example. For some reason I can't get this to work. It rejects me from going to any of the states and when I try to log the error in the run function it responses with 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();
}
]);
您缺少 firebase 依赖项:
var app = angular.module('app', ['ui.router', 'firebase']);