我很难做 requireAuth 工作。我已经尝试过 ngroute 和 uirouter,如果用户没有登录,我只想将他重定向回主页。
在我的应用程序中,我有一个包含多个控制器的页面,我想在其中设置上述规则。
这是工厂和运行方法:
app.factory("AuthFactory", ["$firebaseAuth", function($firebaseAuth) {
var ref = new Firebase("https://torrid-heat-237.firebaseio.com");
return $firebaseAuth(ref);
}
]);
// for ui-router
app.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("/");
}
});
}]);
的,我写了一个 .state 方法:
app.config(["$stateProvider", function ($stateProvider) {
$stateProvider
.state('geniuses', {
url: '/geniuses',
abstract: true,
controller: 'GetAllGeniuses',
templateUrl: "views/listAllgeniuses",
resolve: {
"currentAuth": ["AuthFactory", function(AuthFactory) {
return AuthFactory.$requireAuth();
}]
}
}).state('geniuses', {
url: '/geniuses',
abstract: true,
controller: 'SearchAGenius',
templateUrl: "views/listAllgeniuses",
resolve: {
"currentAuth": ["AuthFactory", function(AuthFactory) {
return AuthFactory.$requireAuth();
}]
}
})
}]);
最后,在两个控制器中,我都在等待 AuthFactory.requireAuth 解决。但是,当我在没有登录的情况下点击 url 时会发生什么,它会自行停留在那里,即使我登录时,它也会显示相同类型的页面..
我在这里做错了什么?