Hello,
The server generating a token for every response.
So, I have a function to get token.
TokenRequest() {
let body = "grant_type=password&username=kaan&password=123";
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
headers.append('Accept', 'application/json');
return this.http.post(this.rootURL+'token', body, { headers: headers })
.map(res => res.json())
}
for example; I want to sign in. First, I should generate a token key, later should check user using token key.
CheckLogin (username: string, password:string):Observable<Response> {
this.TokenRequest().subscribe((token: any) => {
let header = new Headers();
header.append('Content-Type', 'application/x-www-form-urlencoded');
header.append('Accept', 'application/json');
header.append('Authorization', "Bearer " + token.access_token);
let options = new RequestOptions({
headers: header
});
let loading = this.loadingCtrl.create({
content: 'Doğrulama yapılıyor...',
spinner: 'dots'
});
loading.present();
this.response = this.http.get(this.apiURL + 'user/basiclogincheck?username=' + username + '&password=' + password, options)
.map(res => res.json());
return this.response;
}, (err) => {
this.toastService.SendToastMessage("Token oluşturulamadı.", "top", 3)
});
return this.response;
}
So, I have to use nested post/get methods.
but theese methods working async so it’s trying to return the second request without waiting for the answer. then it always returns null data before the answer comes.
how can i return the second request data