At the lowest level, I have a service called AuthHttpService
, which takes the request, attaches auth headers, and then calls the server.
Each component has its own data service, so if I had a StudentComponent, it would have a StudentService, which would call
getStudents(): Observable<Student[]> {
return this._authHttpService.get('/Students')
.map((response: Response) => <Student[]> response.json());
}
So in the component, I would call (for example)
this._studentService.getStudents().subscribe( data => { this.students = data} );
All of this works wonderfully, and has for months. **However, today I'm trying to implement a global redirect on a status of 401 **
How can I go about implementing this in my AuthHttpService
?
In AuthHttpService, I tried replacing the return this.http.get(url, {headers: this.headers})
with
this.http.get(environment.apiUrl + '' + url, {headers: this.headers}).subscribe(r => {
if (r.status !== 401) {
return r;
} else {
this._router.navigateByUrl(loginUrl)
}
});
but now the .map
in the component services is failing, because i'm no longer returning an observable, but instead I'm returning the actual response.