I am working on angular 4 Project with Asp.NET Web API 2. I want to check if the user is authorized before he enters the admin section. I tried to set the resolver in the routing of angular, like that:
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
return this.profile.getProfile()
.map(result => { setAuthorized(); return result;})
.catch(res => { return Observable.of(new Profile()); });
}
setAuthorized() function will set the authorization in a global variable so it can be checked with the function isAuthorized() and the authorization guard:
if (this.route.firstChild != null) {
return this.route.firstChild.data.map( res => {
if (isAuthorized()) {
return true;
}
});
} else {
return Observable.of(false);
}
the routing is:
const routes: Routes =
[
{path: ':lang', component: LanguageComponent,
children: [
{ path: 'dashboard', component: DashboardComponent, resolve: { authority: AuthorityResolverService} },
{ path: 'admin', component: AdminComponent, canActivate: [AuthorizedGuard], resolve: { authority: AuthorityResolverService}},
]}
];
Everything seems to be working correctly. But the problem is when I click F5 or when I copy the URL and open it in a new window, I will be always redirected to the error page. when I monitor the workflow, it shows that it did not call the resolver when I am trying to reach the URL directly:
localhost/admin
I am not receiving any error, but I am being redirected always. Any Ideas?