I'm trying to work with Auth guards in angular. I have a httpcall that sets a true/false value based on a response back from an HTTP call. The problems are: 1) httpClients return an observable 2) the httpClient subscription needs to happened before the Authorized method gets called, that way the hasSessionFlag is already set.
DUAL SERVICE .TS
hasSession() {
return this.http.get<{}>('API CALL', {
withCredentials: true,
observe: 'response'
}).subscribe((res=>{
if(res.status === 200) {
this.hasSessionFlag = true;
} else {
this.hasSessionFlag = false
}
}));
}
//Check if all the logical conditions of the user sessions pass*
isAuthorized(): boolean {
if (this.hasSessionFlag) {
this.authService.login();
} else {
this.dualLoginRedirect();
}
}
canActivate(): boolean {
return this.isAuthorized();
}
ROUTER.TS
{
canActivate: [DualLogonService],
path: 'test',
component: TestPageComponent
}