How to have a route guard that routes to an auxiliary route after passing?

Discussão em 'Angular' iniciado por Alex Davis, Dezembro 11, 2024.

  1. Alex Davis

    Alex Davis Guest

    I have a section of my Angular app that is only accessible through a generated link sent to the user's email (a Docusign type thing). It is sectioned off from the rest of the app via a feature module, with it's own routes, and is lazily loaded in the main app.routes.ts.

    The user will get a link like {domain}.com/sign-doc/document/:id, where :id will be a unique string of characters.

    I wanted to set up the feature module routing structure like this

    export const routes: Routes = [
    {
    path: 'document/:id',
    component: SignDocumentComponent,
    canActivate: [isValidDocumentIdGuard],
    children: [
    {
    path: 'metadata',
    component: DocumentMetadataComponent,
    outlet: 'sign'
    },
    {
    path: 'confirm-identity',
    component: ConfirmIdentityComponent,
    outlet: 'sign'
    },
    {
    path: 'view-document',
    canActivateChild: [ identityConfirmedGuard ],
    children: [
    {
    path: 'signed',
    component: ViewAndSignComponent,
    outlet: 'sign'
    },
    {
    path: 'unsigned',
    component: ViewAndSignComponent,
    outlet: 'sign'
    },
    ],
    },
    ],
    },
    {
    path: 'contact-admin',
    component: ContactAdminComponent,
    },
    ];


    The SignDocumentComponent template looks like this:

    <router-outlet name="sign" />
    <app-session-extender />


    and the app.routes.ts looks something like this:

    export const routes: Routes = [
    /** other routes */,
    {
    path: 'sign-doc',
    loadChildren: () => import('./sign-doc/sign.module').then(x => x.SignDocumentModule),
    }

    ];


    I have a route guard, isValidDocumentIdGuard, which will asynchronously check if the provided id is one we have a document for, and if it is valid should route the auxiliary outlet to metadata, otherwise it should redirect the primary outlet to the contact-admin path. The way I did this was to have the guard return createUrlTreeFromSnapshot(routeSnapshot, [{ outlets: { sign: ['metadata'] } }]) if successful.

    The problem is that I seemingly get caught in an endless loop of entering sign-doc/document/:id/(sign:metadata).

    I have re-created a minimal example on StackBlitz.

    Any help would be greatly appreciated.

    Continue reading...

Compartilhe esta Página