I have an app which uses a login cookie which is sent with most api requests/calls to verify the user. (It is set by the server after a successful login.)
The problem is, that my app is doing a bunch of api calls simultaniously at startup which leads to calls with invalid session ids which are then recreated serverside for each of those calls, so they end up with different session ids => the user get's logged out.
My plan is to send a single api call before each others are allowed. But somehow i can't get it to work in the correct order.
I came up with a promise which will be resolved after the first call has finished in my wrappers constructor like so
private firstCallMade: Promise<any>;
constructor(
private http: HttpClient,
private settings: SettingsProvider)
{
this.settings.getAsyncString("apiEndpoint").then(res =>
{
this.apiUrl = res;
this.firstCallMade = new Promise((resolve, reject) =>
{
this.http.get(this.apiUrl + this.firstCallEndpoint, { withCredentials: true })
.subscribe(
(result) => {
this.logger.system(this, 'First Call', `First call successful: ${JSON.stringify(result)}`);
resolve();
},
(error) => {
this.logger.system(this, 'First Call', `First call failed: ${JSON.stringify(error)}`);
resolve();
});
});
});
}
And in the other wrapper methods i use it like so
get<T>(endpoint: string): Observable<T>
{
return new Observable<T>(subscriber =>
{
this.firstCallMade.then(_ =>
{
this.http.get<T>(this.apiUrl + endpoint)
.subscribe(
next => subscriber.next(next),
error => subscriber.error(error));
});
});
}
But this does not work.
Is my code wrong?
EDIT FOR CLARIFICATION What is want is that all, but the first call can go simultaniously, after the first call has finished (thus setting the right cookie data for subsequent calls).