0

After login or registration, I want to create a class that will determine the role of the authenticated user and redirects him to the correct page. Is there another way of doing this rather than using an angular component? What I don't like about the component page is that the page is loaded before redirection, even though the component doesn't display anything, it still uses the base template from app.component.

I'm using Keycloak for registration and login (meaning it's not part of the app I'm developing and I can only set the redirectUri). So I can only redirect to one URL.

4
  • You must have a login component right? Add logic in that component to determine which page to land next. Commented May 18, 2018 at 6:18
  • 1
    In theory you could redirect from any class like a service or a guard or what ever you like Commented May 18, 2018 at 6:20
  • Just create Global service for the same and navigate from there , isn't it? Commented May 18, 2018 at 6:20
  • @HarryNinh, I'm using keycloak and it redirects to a certain component. Since it's a component, it loads the template from app.component. That is what I want to avoid, because it looks ugly. Commented May 18, 2018 at 6:36

1 Answer 1

1

Here is the general idea I used for this case.

Create a login component which has a blank template (.html).

Make sure to set the routing for it as well.

app.routing.ts

{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'login', component: LoginScreenComponent},

login-screen.component.ts

constructor(private keyCloakService: KeyCloakService, private router: Router) {}
ngOnInit() {
      // Do a call where you get logged in user and it's role, ex:
      let role = this.keyCloakService.getUserRole(); 

      // Do a redirect based on that role
      if(role == "admin){
           this.router.navigate(["/admin"]);
      }

  }

Once you get this working, I would also suggest adding AuthGuard to your role routes.

There is also second, more clean, approach which you might take, but is a bit more complicated if you didn't work with AuthGuard before.

In short, you would handle the solution I gave you in your AuthGuard service. That way you will avoid creating a login component, but the outcome will be the same.

And regarding the template of app.component.ts, the common practice is that you only have <router-outlet></router-outlet> in it, therefore you don't load any layout, and you have more freedom over it.

EDIT (Answer to question below): You can use your secure and non secure layout through routing. For instance:

  {
    path: 'admin',
    component: AdminLayoutComponent,
    canActivate: [Guard],
    children: [
      {
        path: '',
        loadChildren: './components/app-admin/secure.module#SecureModule'
      }
    ]
  }

And do the same thing for public layout. That way you will have only in your app.component, and will have much more freedom for redirects, etc.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I'm going for different AuthGuard for registration and login. Regarding app.component, the reason why it's not just outlet code in there, is I have separate template for public and secured pages. So I head headers, footers, etc. Other than that, the other way is to specified them on each page right? Or are there other ways?
The fact that you have separate template for public and secured pages doesn't really matter, you can still have only outlet code in app.component. Check my edited answer to see how to handle it then.
I'm accepting this answer, as it guides me in the right direction. For those wondering how my project ended up - I describe it here: czetsuya-tech.blogspot.com/2018/05/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.