Previously I have setup my Services like so:
post(user: User): Observable<User> {
const options = new RequestOptions({headers: this.headers});
return this.http.post('/api/auth', JSON.stringify(user), options)
.map(response => {
this.accessToken = response.headers.get('x-access-token');
return response.json()
})
.catch(handleError);
}
Which is useful because the calling Component can then easily:
this.authService.post(user).subscribe(user => {
if (!this.authService.isLoggedIn()) {
this.alertsService.alerts.push({msg: JSON.stringify(user), type: 'warning'});
return;
}
this.alertsService.alerts.push({msg: `Logged in with ${this.authService.accessToken}`, type: 'success'});
this.router.navigate(['/contacts']);
}, error => this.alertsService.alerts.push({msg: error, type: 'danger'}));
I want to move to the new HttpClient ASAP, because of your retry features and especially the reintroduction of interceptors from AngularJS. Then my auth pipeline will work with less hacks!