I am trying to implement two sequential HTTP requests in my Angular 2 App. When the user click the login button I want to save in LocalStorage the refresh and access tokens and then execute one more HTTP request in order to get User info. I saw this post but it doesn't work in my case. Angular 2 - Chaining http requests
This was my initial implementation for login and getMe service: How can I merge the requests bellow?
login.component.ts
this.authenticationService.login(this.user.email, this.user.password)
.subscribe(
data => {
// this.alertService.success('Registration successful', false);
this.router.navigate([this.returnUrl]);
},
error => {
console.log(error);
this.alertService.error(error);
this.loading = false;
});
authentication.service.ts
login(email: string, password: string) {
let headers = new Headers({
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
'Cache-Control': 'no-cache'
});
let options = new RequestOptions({ headers: headers });
return this.http.post(this.config.apiUrl + this.authenticationUrl + '/login',
JSON.stringify({ email: email, password: password }), options)
.map((response: Response) => {
let tokens = response.json();
if (tokens && tokens.token && tokens.refreshToken) {
// store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('tokens', JSON.stringify(tokens));
}
});
}
user.service.ts
getMe() {
console.log('getMe');
return this.http.get(this.config.apiUrl + this.usersUrl + '/me', this.jwt()).map((response: Response) => {
let user = response.json();
if (user)
localStorage.setItem('currentUser', JSON.stringify(user));
});
}