I'm trying to make a system in angular that navigates to the login page if the server returns an 401 error, I tried this but it doesn't navigate:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Router } from '@angular/router';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
@Injectable()
export class AuthService {
private heroesUrl = 'http://localhost:8000/auth/loggedIn';
constructor (private http: Http, private router: Router) {}
loggedIn (): Observable<string|number> {
return this.http.get(this.heroesUrl)
.map(res => { return res.json(); })
.catch(this.handleError);
}
private handleError (error: Response|any) {
if (error.status == 401) {
this.router.navigate(['/path_to_login_page']);
return error.status;
}
}
}
And I already checked the response from the server and I also console logged error.status == 401, and it did return true, can anyone help me with this problem.
Thanks in advance