There's a really good chance I don't know what I'm talking about here, and perhaps I'm doing this all wrong. Hopefully someone with far more web knowledge than me can point me in the right direction.
I built a very simple Angular2 service that returns promises and issues web requests.
Here's an example:
postAddUser(username, password, email): Promise < SimpleAppARenoAPITokenResponse > {
return this.http.post(this.rpcUrl, JSON.stringify(this.makeAddUser(username, password, email)), {
headers: this.headers
})
.toPromise()
.then(res => res.json() as SimpleAppARenoAPITokenResponse)
.catch(this.handleError);
}
Then in the handleError I've been checking for the "error.status" of 401, if it is I'd like to redirect to the /login page. For that I'm doing:
private handleError(error: Response | any) {
// In a real world app, we might use a remote logging infrastructure
let errMsg: string;
console.log(error);
if (error.status == 401) {
console.log('gets here');
this.router.navigatebyUrl('/login');
}
}
Now I get a 'gets here'
in the console, but it's like nothing happens when I call the router command. I've injected router and route into the service and that seems to work, just can't figure out how to redirect during the catch.
If I call navigatebyUrl
in the postAddUser
function it works fine. My guess is it has something to do with Promises inside Promises or the like. However I'm still struggling to understand all this with my dinosaur win32 brain.
Thanks for any suggestions you could provide.