In my angular2 application, I do have authentication.service.ts and login.component.ts
authentication.service.ts
login(username: string, password: string) {
return this.http.post('/auth/loginapp', JSON.stringify({ username: username, password: password }))
.map((response: Response) => {
// login successful if there's a jwt token in the response
let user = response.json();
//Printt the backend response to the console
this.logger.debug('User '+ JSON.stringify(user));
if (user && user.token) {
// store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify(user));
}
});
}
authentication.service.ts calls to backend authentication services and get the response according to user input and save to the localStorage.
login.component.ts
this.authenticationService.login(this.model.username, this.model.password)
.subscribe(
data => {
this.logger.debug("Login User"+ JSON.stringify(data))
this.router.navigate(['register']);
},
error => {
this.logger.error(error);
this.alertService.error(error);
this.loading = false;
});
I am always getting data as undefined in login.component.ts. authentication.service.ts gets the response correctly. I need to get the response data to the login.component.ts. could anyone tell me the issue with my code?