I have a application and i'm checking for authorisation to protecting route using AuthGuard.
Now i want to protect my child routes with autguard based on role system which i'm getting response from backend while logging in. If role is admin i will allow him to view all routes. If role is user i want him to access only notification route. I tried using *ngIf but i will hide only route, but when user enter URL it will be redirecting
auth.guard.ts
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
const isAuth = this.authService.getIsAuth();
if (!isAuth) {
this.router.navigate(['/sign-in']);
}
return isAuth;
}
roleAuth.guard.ts
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
const role = this.authService.role();
if (role ==='Admin') {
//got stuck here
}
if (role ==='moderator') {
//got stuck here
}
if (role ==='user') {
//got stuck here
}
return role;
}
app-routing.ts
const routes: Routes = [
{ path: 'sign-in', component: SignInComponent },
{
path: '',
canActivate: [AuthGuard],
component: SideNavComponent,
children: [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{
path: 'dashboard',
loadChildren: () => import('./components/sidenav/pages/dashboard/dashboard.module')
.then(m => m.DashboardModule)
},
{
path: 'notifications',
loadChildren: () => import('./components/sidenav/pages/notifications/notifications.module')
.then(m => m.NotificationsModule)
},
],
},
];