[英]JHipster, AngularJS- After login, based on user authority how to take user to different pages
new to both AngularJS & JHipster and appreciate any help. 是AngularJS和JHipster的新功能,感谢您的帮助。
I currently have two user authorities, ROLE_ADMIN, and ROLE_USER. 我目前有两个用户权限,ROLE_ADMIN和ROLE_USER。 After logging in I would like to be able to direct the user to different pages (that they have access to). 登录后,我希望能够将用户定向到其他页面(他们可以访问)。 I believe I have been able to narrow down the issue to my sign in controller: 我相信我已经能够将问题缩小到我登录控制器的范围:
function login (event) {
event.preventDefault();
Auth.login({
email: vm.email,
password: vm.password,
rememberMe: vm.rememberMe
}).then(function () {
vm.authenticationError = false;
$state.go('home', {}, {reload: true});
$rootScope.$broadcast('authenticationSuccess');
// previousState was set in the authExpiredInterceptor before being redirected to login modal.
// since login is successful, go to stored previousState and clear previousState
if (Auth.getPreviousState()) {
var previousState = Auth.getPreviousState();
Auth.resetPreviousState();
$state.go(previousState.name, previousState.params);
}
}).catch(function () {
vm.authenticationError = true;
});}
If I change $state.go('home', {}, {reload: true});
如果我更改$state.go('home', {}, {reload: true});
to $state.go('trips', {}, {reload: true});
到$state.go('trips', {}, {reload: true});
('home' -> 'trips'), or change that to another page it works fine, so long as that user has the proper privileges, if not it goes to an error page. ('home'->'trips'),或者将其更改为另一个页面,只要该用户具有适当的特权,就可以正常工作;否则,它会转到错误页面。
Intuitively, I would like to add something like: 直观地说,我想添加以下内容:
if (authority === 'ROLE_ADMIN') {
$state.go('home', {}, {reload: true});
} else if (authority === 'ROLE_USER') {
$state.go('trips', {}, {reload: true});
}
But I cannot find a way to access what authority I am in/using. 但是我找不到一种方法来访问我正在/正在使用的权限。 I have also looked in auth.service.js and logged about everything I could think of to see if it is lurking in there somewhere. 我还查看了auth.service.js,并记录了我可能想到的所有内容,以查看它是否潜伏在某个地方。
If anyone has any suggestions or can point me in the right direction that would be very helpful! 如果有人有任何建议或可以指出正确的方向,那将非常有帮助! Thanks! 谢谢!
stumbled across a solution with the help of a friend and wanted to share in case anyone else had the same issue... or, even better, a more elegant solution. 在朋友的帮助下偶然发现了一个解决方案,并希望在其他人遇到相同问题的情况下进行分享……甚至更好的是,一个更优雅的解决方案。
Looking into what Principal
was doing in some of the other controllers found that result
will return true
or false
查看Principal
在其他一些控制器中的操作后发现result
将返回true
或false
Principal.hasAuthority('ROLE_XYZ').then( function(result) {
if(result) {
$state.go('trips', {}, {reload: true});
}
Principal.hasAuthority('ROLE_QWEQWE').then( function(result) {
if(result) {
$state.go('home', {}, {reload: true});
}
As a result I just repeated and replaced ROLE_XYZ
with the authority in question and that routes me to the page I am looking to get to. 结果,我只是重复了一次,并用有问题的权限替换了ROLE_XYZ
,这将我带到了我要访问的页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.