I need a way to safeguard my routes by verifying if a user is logged in from the server, but I'm facing issues with asynchronous functions not executing properly.
Below is the code snippet that's causing trouble:
canActivate (route: ActivatedRouteSnapshot , state: RouterStateSnapshot): Observable<any> {
let permission = route.data[ "permission" ];
if(permission){
..execute another function which works
)
}else{
return this.checkLogin();
}
}
Regarding the checkLogin
function:
checkLogin (url?: string): any {
this.authService.checkLoggedin()
.subscribe(
res=>{
if(res){
return true;
}else{
this.router.navigate ( [ '/login' ] );
return false
}
}
)
}
Moving on to the auth service:
checkLoggedin():Observable<any> {
//validate user's login status using server call
return this._httpclient.get(this.authurl + "/default/is-loggedin")
.map(res => {
return res.json().data;
},error=>{
return Observable.of(false);
}
);
}
Could someone point me in the right direction regarding where I might be making mistakes?