I'm not able to set item in local storage when the response from backend is 200. Seems like it doesn't recognize the status of the request.
options = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Authorization': 'Bearer '+localStorage.getItem("access_token")
}),
observe: "response", // to display the full response
responseType: "json"
};
constructor(private http: HttpClient, private router: Router) {
}
authenticate(url, data, options) {
this.http.post(environment.getBaseAddress() +url+'/', data, options).subscribe((data:any) => {
if (data.status == 200) {
localStorage.setItem("access_token", data['access_token']);
console.log(data.status)
}
else this.router.navigateByUrl('something-else');
})
}
The fact that the next callback of the subscription block gets executed implies that your API call is successful. So you don't need to explicitly check the status code, you can alternatively do:
this.http.post(environment.getBaseAddress() +url+'/', data, options)
.subscribe((data:any) => {
localStorage.setItem("access_token", data['access_token']);
}, (err) => {
this.router.navigateByUrl('something-else');
});
More on subscription callbacks.