我有此错误处理功能,可响应路由中的错误,并在用户未身份验证时将用户重定向到目标网页:
core.run(['$rootScope', '$location', function($rootScope, $location) {
$rootScope.$on('$routeChangeError', function(event, next, previous, error) {
if (error === 'AUTH_REQUIRED') {
$location.path('/');
}
});
}]);
现在我要反过来,以便在用户通过身份验证时,此路由不可用:
.when('/', {
templateUrl: 'views/pages/landing.html',
resolve: {
'isAuth': ['fbRefs', function(fbRefs) {
return fbRefs.getAuthObj().$waitForAuth();
}]
}
})
如何更改上面的resolve
以检查用户是否进行了身份验证,如果是,则重定向到/home
而不是/
?
我尝试手动拒绝从解析返回的承诺,但无论用户是否经过身份验证,都会禁用该路由。
答案 0 :(得分:1)
您可以使用$ q
手动处理它.when('/', {
templateUrl: 'views/pages/landing.html',
resolve: {
'isAuth': ['fbRefs', function(fbRefs, $q) {
var onLoggedIn = function() {
return $q.reject('NO_AUTH_REQUIRED');
}
var onLoggedOut = function(fbData) {
return fbData;
}
return fbRefs.getAuthObj().$requireAuth()
.then(onLoggedIn, onLoggedOut);
}]
}
})
...
core.run(['$rootScope', '$location', function($rootScope, $location) {
$rootScope.$on('$routeChangeError', function(event, next, previous, error) {
if (error === 'AUTH_REQUIRED') {
$location.path('/');
} else if (error === 'NO_AUTH_REQUIRED') {
$location.path('/home');
}
});
}]);