Hello, I'm facing an issue with making a post request in Angular 5. The token I retrieve seems correct as it works fine when tested with Postman. Can someone provide me with a hint or suggestion on what could be going wrong?
AuthService.ts
getProfile(){
this.loadToken();
let headers = new HttpHeaders();
headers.append('Authorization', this.authToken);
headers.append('Content-Type', 'application/json');
return this._http.get('http://localhost:8080/users/authenticate', {headers: headers}).map(res => res);
};
loadToken() {
console.log(this.authToken); //undefined here
this.authToken = localStorage.getItem('id_token');
console.log(this.authToken) // correct token here
}
ProfileComponent.ts
ngOnInit() {
this._authService.getProfile().subscribe(profile => {
console.log('1', profile);
this._router.navigate(['/profile'])
}, err => {
console.log(`Error`, err); // IT GOES STRAIGHT HERE
return false;
});
}
//ERROR OUTPUT
HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: "Unauthorized", url: "http://127.0.0.1:8080/users/profile", ok: false, …}