Hello, I am writing a service that consumes an API. The API requires a token be present on requests so that the user can be authenticated. Upon user login a token is given to the app which I store using the Storage module. Then I’m accessing it in all services which consume the API.
The problem is that the code which I’ve put in the constructor to load the token and id required for the authentication is asynchronous and so when the service is called it first executes the given method (getTask for example) and then the Token and Id get loaded in from the Storage module. So is there a way to wait for the token and id then start executing methods, or should I think of another way to do this?
Here is the constructor code:
constructor(private http: Http, private storage: Storage) {
this.storage.get('token')
.then(token => {
console.log('Got token', token);
this.headers.append('Token', token);
this.storage.get('user')
.then(
user => {console.log('Got id', user.id);this.headers.append('Id', user.id)},
error => console.error(error)
);
}, error => console.error(error));
}