0

I'm making a JHipster project and I need to show a different home page for each role that I log in with, I'm using Angular 1.x. For example I have the ROLE_ADMINand the ROLE_USERand I need to show a different dashboard for each on.

I have read that I can put something like this in the home.controller.js

  this.eventManager.subscribe('authenticationSuccess', (message) => {
            this.principal.identity().then((account) => {
                if (account.authorities.indexOf("ROLE_ADMIN") >=0)
                {
                    this.router.navigate(['#/pages/prueba/prueba.html']);
                }
                else
                {
                    this.account = account;
                }
            }); 
         });

But I can't make it work, it shows this error: Error: this is undefined

Anyone have a clue about this?

Fer
  • 1
  • I guess you're a beginner in JS, please read https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work to understand,why `this`is undefined in your context. – Gaël Marziou Nov 09 '17 at 12:04

1 Answers1

0

You can have a look at auth.service.js. There is a method called authorize, which in turn calls authThen. These methods are invoked after the user is authenticated and normally redirects the user to the last state (normally the protected state that failed, since the user was not authenticated and therefore was redirected to the login). You may change the code here to redirect the user according to its authorities. The same methods (authorize and authThen) are also called everytime before a state changes, because it is a "resolve" for each state (have a look at the app.state.js).

Another option would be to add an "onEnter" function to your state definition that redirects to the appropiate view.

Indivon
  • 1,433
  • 13
  • 28