I need to redirect into different user page depends on the userRole value received from the header.
angular.routing.ts
{ path: '', pathMatch: 'full', redirectTo: '/login' },
{ path: 'user', loadChildren: './home/home.module#HomeModule', canActivate: [AuthGuard], data: { roles: Role.User} },
{ path: 'admin', loadChildren: './somemodule#SomeModule', canActivate: [AuthGuard], data: { roles: Role.Admin}},
{ path: 'login', component: LoginComponent, canActivate: [RandomGuard] }
Initially I'm redirecting into LoginComponent, CanActive random.guard.ts is a API call to get the header details from server.
random.guard.ts
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.loginService.isHeader().pipe(
map(e => {
// the problem is e.headers.get('userRole') undefined
if (e.headers.get('userRole') === 'user') {
this.router.navigate(['/user']);
} else if(e.headers.get('userRole') === 'admin') {
this.router.navigate(['/admin']);
} else {
return true;
}
}),
catchError((err) => {
this.router.navigate(['/login']);
return of(false);
})
);
}
loginservice.ts
isHeader(): Observable<boolean> {
return this.http.get(`${environment.baseUrl}home/login`,{observe: 'response'}).pipe(
map((response: any) => {
return response;
// response i did't get header value
// how to receive header value on here
})
);
}
If i subscribe the http get call, i will get the header value. How to refactor the code and recieve he header value.