I have an angular 5 application where I do check if the user is authenticated or not. For that I'am using the AuthGuard feature. It works fine.
My issue is that the server some times doesn't send any clear status back to the client. In other words, there is no server error such 403
or 401
etc.
The only thing I can see on Chrome developer tool in Network is the status: pending
which is not an error for my script... therefore the page which is a kind of admin-page, is not being loaded and I get a blank page.
I have been searching but unfortunately the only topics I found regarding pending
status issues is e.g. these ones which couldn't really be a help... and the last one is nearly 5 years old question.
Android InApp Purchase - How to handle Pending Status?
jQuery ajax callback in pending status
How to fix the “pending” status in Chrome Developer Window?
Before trying to find out what could be the exact reason for that (as I still have no real idea about that), I first would like to catch this case and redirect the user to the home or register page for example.
Here is the code where I send the request to check user authentication:
Would .finally( () => { .... })
be an option and does it make sense in this case based on the following proposed solution?
service:
...
public getData() {
if (this.isAuthenticated === true) {
return Observable.of(true);
} else {
return this._http.get(url, {withCredentials: true, observe: 'response'})
.map((res) => {
if ( res.status === 200) {
this.isAuthenticated = true;
return true;
} else {
return false;
}
}, (err) => {
return false;
});
}
}
guard:
....
constructor() {private bckendData: Myservice, ...}
....
canActivate( route: ActivatedRouteSnapshot, ... ): Observable<boolean> | Promise<boolean> | boolean {
return this.bckendData.getData()
.map( (res) => {
if ( res === true) {
return true;
} else {
doSomething....
return false;
}
})
.catch(err => {
doSomething....
return Observable.of(false);
});
}