I'm upgrading to ui-router@1.0.0-beta.2
and trying to figure out what is the best way to redirect user to a different state if he doesn't have necessary permissions.
我正在升级到ui-router@1.0.0-beta.2并尝试找出如果用户没有必要的权限,将用户重定向到其他状态的最佳方法。
This is what I have:
这就是我所拥有的:
.state('company_dashboard', {
url: '/dashboard',
templateUrl: 'dashboard.html',
controller: 'CompanyDashboardCtrl',
resolve: {
user: function(UserService) {
return UserService.getActiveUser();
},
company: function(UserService) {
return CompanyService.getActiveCompany();
},
permissions: function(CompanyService, user, company){
return CompanyService.getUserPermissions(company, user);
}
},
onEnter: function($state, permissions) {
if (permissions.canAccessDashboard === false)
return $state.go('company_landing_page');
}
})
The above example works, but it logs the following error: TransitionRejection(type: 2, message: The transition has been superseded by a different transition, detail: Transition#1( ''{} -> 'company_landing_page'{} ))
which makes me think, there should be a better way.
上面的示例有效,但它记录了以下错误:TransitionRejection(类型:2,消息:转换已被不同的转换取代,详细信息:转换#1(''{} - >'company_landing_page'{}))让我想到,应该有一个更好的方法。
Side question, is it the good practice to check permissions during the resolve
?
附带问题,在解决过程中检查权限是一种好习惯吗?
UPDATE:
Changing $state.go()
to $state.target()
helped me to get rid of the console error. This comment has helped. Though the question stands, whether I'm doing it it the right place?
将$ state.go()更改为$ state.target()帮助我摆脱了控制台错误。这条评论有所帮助。虽然问题在于,我是否正确地把它做到了正确的地方?
1 个解决方案
#1
1
I would add a data property to your state as follows:
我会向您的州添加一个数据属性,如下所示:
.state('company_dashboard', {
data: {
requiresAuthentication: true
}
}
And then:
app.run(function ($rootScope, $state) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
// Check if user is authenticated
// And route requires authentication
if (!toState.data.requiresAuthentication) {
$state.go(toState.name, toParams);
} else if (user && toState.data.requiresAuthentication) {
$state.go(toState.name, toParams);
} else {
$state.go('login');
}
});
});
This seems to work quite well even if I am sure it can be more polished, but I hope this gives you an idea on how you may proceed.
这似乎工作得很好,即使我确信它可以更加精致,但我希望这可以让你了解如何进行。
#1
1
I would add a data property to your state as follows:
我会向您的州添加一个数据属性,如下所示:
.state('company_dashboard', {
data: {
requiresAuthentication: true
}
}
And then:
app.run(function ($rootScope, $state) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
// Check if user is authenticated
// And route requires authentication
if (!toState.data.requiresAuthentication) {
$state.go(toState.name, toParams);
} else if (user && toState.data.requiresAuthentication) {
$state.go(toState.name, toParams);
} else {
$state.go('login');
}
});
});
This seems to work quite well even if I am sure it can be more polished, but I hope this gives you an idea on how you may proceed.
这似乎工作得很好,即使我确信它可以更加精致,但我希望这可以让你了解如何进行。