G
G
Ghoulll2020-03-04 13:51:31
Angular
Ghoulll, 2020-03-04 13:51:31

How to implement role model in Angular?

How to correctly implement routes depending on the role? At the moment, the universal page is news where all authorized users go, but users with a certain role must go to another section.
cabinet.routing.ts file:

const cabinetRoutes: Routes = [
  { path: '', redirectTo: 'news', pathMatch: 'full' },
  { path: '', component: LayoutComponent, children: [
      { path: 'news', loadChildren: () => import('./modules/news/news.module').then(m => m.NewsModule) },
      { path: 'analytics', loadChildren: () => import('./modules/analytics/analytics.module').then(m => m.AnalyticsModule) },
      { path: 'declaration', loadChildren: () => import('./modules/declaration/declaration.module').then(m => m.DeclarationModule) },
      { path: 'directory', loadChildren: () => import('./modules/directory/directory.module').then(m => m.DirectoryModule) },
      { path: 'documents', loadChildren: () => import('./modules/documents/documents.module').then(m => m.DocumentsModule) },
      { path: 'reestr', loadChildren: () => import('./modules/reestr/reestr.module').then(m => m.ReestrModule) },
      { path: 'orgInformation', loadChildren: () => import('./modules/org-information/org-info.module').then(m => m.OrgInfoModule) },
      { path: 'dashboard', loadChildren: () => import('./modules/dashboard/dashboard.module').then(m => m.DashboardModule) },
      { path: 'admin', loadChildren: () => import('./modules/admin/admin.module').then(m => m.AdminModule) },
    ],
  },
];

transfers the authorized user to the cabinet
auth.service.ts file:
...
          this.authService.sendSignedPhraseToServer(this.signedCert).subscribe( responseWithJWT => {
            localStorage.setItem('someKey', responseWithJWT.token);
            this.user = this.authService.getToken;
            this.user = jwt_decode(this.user);
            this.router.navigate(['/cabinet']);
          });


app-routing module:
const appRoutes: Routes = [
  { path: '', pathMatch: 'full', redirectTo: 'cabinet' },
  {
    path: 'auth',
    loadChildren: () => import('./modules/auth/auth.module').then(m => m.AuthModule),
  },
  {
    path: 'cabinet',
    loadChildren: () => import('./modules/cabinet/cabinet.module').then(m => m.CabinetModule),
    canLoad: [AuthGuard],
  },
];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2020-03-04
@Ghoulll


How to write a routing so that when .....

"Routing" is the relationship between the route and the corresponding component.
.... to have a redirect to different pages during authorization?

Here at authorization and navigate where it is necessary.
If you want cabinet to be a universal route and at the same time a laziload, then lazis will have to be nested in it, i.e. additional subroutes. The list of routes itself does not provide any logic.
But the logic can be placed in a guard or a resolver. This is actually middleware in the router.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question