I'm creating application with separate dashboards for different client types (businessClient, individualClient). Client type comes with session context, so it is known before navigation to dashboard starts. I would like to load proper dashboard based on client type using angular router mechanisms.
My current implementation:
- separate paths for individual and business client
- each path has canActivate guard checking if current client has type based access to specified dashboard
- if client has no access to specified route guard redirects to url specified in route data section
Routing
{
path: 'dashboard',
component: DashboardComponent,
children: [
{
path: '',
pathMatch: 'full',
redirectTo: 'indv-client-data'
},
{
path: 'indv-client-data',
canActivate: [IsProperClientTypeGuard],
resolve: {
clientData: IndvClientDataResolver
},
component: IndvClientDataComponent,
data: {
clientType: ClientType.INDIVIDUAL,
redirectionUrl: 'busi-client-data'
}
},
{
path: 'busi-client-data',
canActivate: [IsProperClientTypeGuard],
resolve: {
clientData: BusiClientDataResolver
},
component: BusiClientDataComponent,
data: {
clientType: ClientType.BUSINESS,
redirectionUrl: 'indv-client-data'
}
}
]
}
Guard
public canActivate(route: ActivatedRouteSnapshot): boolean {
const clientType = route.data['clientType'];
const redirectionUrl = route.data['redirectionUrl'];
if (this._sessionContext.clientType === clientType) {
return true;
} else {
this._router.navigate([redirectionUrl]);
}
}
My solution works well, covering all my needs, but i have a feeling that it's pretty ugly (especially redirectionUrl data parameter). Is there a better way to achieve same result using angular router?