Expressingx
Expressingx

Reputation: 1572

Angular guard componentless route with optional id

I'm working on restricting routes with API call checking if there is permissions for that. Current routes are

{
    path: '',
    children: [
        { path: '', component: HomeComponent},
        { path: ':id', component: HomeComponent },
        { path: 'unauthorized', component: UnauthorizedComponent },
        { path: '**', redirectTo: '' }
    ]
}

What I want is to restrict boh '' and :id with the guard and in it I have re route to unauthorized but if I apply canActivate to the root path it causes circular calls because it redirects to unauthorized which is guarded as well and it blocks the UI. If I apply it to the childs which I want to protect It doesn't get called.

Upvotes: 0

Views: 176

Answers (1)

user4676340
user4676340

Reputation:

{
    path: '',
    children: [
        { path: '', canActivate: [WhateverService], children: [
          { path: '', component: HomeComponent},
          { path: ':id', component: HomeComponent },
        ]},
        { path: 'unauthorized', component: UnauthorizedComponent },
        { path: '**', redirectTo: '' }
    ]
}

This should do the trick. It's just basic route grouping.

Upvotes: 1

Related Questions