angularangular2-routingroles

Redirect to first allowed route in Angular 2?


I have following route configuration:

const routes: Routes = [
  { path: '', redirectTo: 'customers', pathMatch: 'full' },
  { path: 'customers', component: CustomerListComponent, canActivate: [CustomerGuard] },
  { path: 'products', component: ProductListComponent, canActivate: [ProductGuard] },
  { path: 'sales', component: SalesListComponent, canActivate: [SalesGuard] }
]; 

The problem is, I have no 'dashbord', only domain management pages, and although every authorized user will have access to at least one of them, none of them will be available to all user.

Is there an option to say: redirect to first route, that can be activated? Or I need to write a virtual component under path '/' that would do dynamic redirection, based on user roles?


Solution

  • There is no way to redirect to the first available route. Using a dummy route with a dummy component that just redirects depending on the role is the way to go. You can also do the redirect in a guard https://angular.io/docs/ts/latest/guide/router.html#!#guards but the dummy route would need a component anyway.

    What you can also do is use router.resetConfig() to load routes depending on the role as shown in Dynamic routing based on external data