I am currently working on a request to my backend system that involves setting up headers and tokens. The code I have written for this process is as follows:
loggedIn() {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
this.getToken().then((val) => {
headers.append('X-Auth-Token', val);
});
const options = new RequestOptions({headers: headers});
let seq = this.api.post('/login', null, options).share();
seq.map((res: Response) => res.json())
.subscribe(res => {
}, err => {
console.log("HEADERS ------- ", options.headers);
});
return seq;
};
However, I have encountered an issue where sometimes the 'X-Auth-Token' header is not being set. I need to find a way to ensure that the header is always being properly set. Any suggestions on how to achieve this?
The implementation of the getToken() function is shown below:
getToken() {
return this.storage.get('token').then((value) => {
return value;
});
}
Finally, here is how the method checkIfLoggedIn(), which triggers the loggedIn() function, looks like:
checkIfLoggedIn() {
this.user.loggedIn()
.subscribe((res) => {
console.log("LoginPage RETURN VALUE --------------- ", res);
}, (err) => {
console.log("LoginPage RETURN ERROR --------------- ", err);
});
}