用auth0s authguard配置角路由

发布于 2025-01-25 04:14:06 字数 1088 浏览 3 评论 0原文

我想配置嵌套的懒惰加载路线,只有在用户登录时才可用。身份验证可与Auth0s一起使用,并且是相应的authguard。 因此,例如,我的路线看起来像这样:


const routes: Routes = [
  {
    path: ':id',
    children: [
      {
        path: '',
        children: [        {
            path: 'first-component',
            loadChildren: () => import('./first-component.module').then((m) => m.FirstComponentModule),
            canLoad: [AuthGuard],
          },
            {
            path: 'second-component',
            loadChildren: () => import('./second-component.module').then((m) => m.SecondComponentModule),
            canLoad: [AuthGuard],
          },
        
        ],
      },
      {
        path: '',
        pathMatch: 'full',
        redirectTo: 'first-component',
      },
    ],
  },
  {
    path: ':id',
    redirectTo: ':id',
    pathMatch: 'full',
  },
  {
    path: '**',
    component: PageNotFoundComponent,
  },
];

:ID < /code> paramter是需要的,应该保留在URL中,以便看起来像这样:

my-app/ID123/first-component

但是身份验证我被重定向到 /id123-&gt;找不到

我想念什么?

I would like to configure my lazy loaded routes nested, which are only available if the users is logged in. Authentication works with Auth0s and it's corresponding AuthGuard.
So for example my routes look like this :


const routes: Routes = [
  {
    path: ':id',
    children: [
      {
        path: '',
        children: [        {
            path: 'first-component',
            loadChildren: () => import('./first-component.module').then((m) => m.FirstComponentModule),
            canLoad: [AuthGuard],
          },
            {
            path: 'second-component',
            loadChildren: () => import('./second-component.module').then((m) => m.SecondComponentModule),
            canLoad: [AuthGuard],
          },
        
        ],
      },
      {
        path: '',
        pathMatch: 'full',
        redirectTo: 'first-component',
      },
    ],
  },
  {
    path: ':id',
    redirectTo: ':id',
    pathMatch: 'full',
  },
  {
    path: '**',
    component: PageNotFoundComponent,
  },
];

the :id paramter is needed, and should stay in the url so it should look like this:

my-app/ID123/first-component

But authentication I am redirected to /ID123 -> not found

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2025-02-01 04:14:06

警卫不应该是问题。

您可以尝试此简化的路由配置:

const routes: Routes = [
  {
    path: 'not-found',
    component: PageNotFoundComponent,
  },
  {
    path: ':id',
    canLoad: [AuthGuard],
    children: [
      {
        path: 'first-component',
        loadChildren: () => import('./first-component.module').then((m) => m.FirstComponentModule),
      },
      {
        path: 'second-component',
        loadChildren: () => import('./second-component.module').then((m) => m.SecondComponentModule),
      },
      {
        path: '**',
        pathMatch: 'full',
        redirectTo: 'first-component',
      },
    ],
  },
  {
    path: '**',
    redirectTo: 'not-found',
  },
];

如果此配置不起作用,请发送您的父路由配置。
这些问题可能存在。

The guard should not be the problem.

You can try this simplified routes configuration:

const routes: Routes = [
  {
    path: 'not-found',
    component: PageNotFoundComponent,
  },
  {
    path: ':id',
    canLoad: [AuthGuard],
    children: [
      {
        path: 'first-component',
        loadChildren: () => import('./first-component.module').then((m) => m.FirstComponentModule),
      },
      {
        path: 'second-component',
        loadChildren: () => import('./second-component.module').then((m) => m.SecondComponentModule),
      },
      {
        path: '**',
        pathMatch: 'full',
        redirectTo: 'first-component',
      },
    ],
  },
  {
    path: '**',
    redirectTo: 'not-found',
  },
];

If this configuration does not work, please send your parent routing configuration.
It is possible that the issues is there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文