简体   繁体   中英

JHipster, AngularJS- After login, based on user authority how to take user to different pages

new to both AngularJS & JHipster and appreciate any help.

I currently have two user authorities, ROLE_ADMIN, and 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}); to $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.

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.

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.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM