I am new to Ionic framework & going to develop an app which utilises rest api from my server. In each API I have to pass a token in http header. If this token is valid then I return response & inflate the listview. If token is not valid then I have to hit another API to generate token & again hit first API to fetch data.
My problem is second case. When token is invalid, it is generate successfully & then calling first API is also successful but this time listview is not inflated. Please help.
home.ts
loadPeople(){
this.dataService.load()
.then(data => {
this.mylist = data;
});
}
data-provider.ts
load() {
if (this.data) {
return Promise.resolve(this.data);
}
return new Promise(resolve => {
let headers = new Headers({ 'token': this.token });
let options = new RequestOptions({ headers: headers });
this.http.get('myurl1', options)
.map(res => res.json())
.subscribe(data => {
if(data.message === 'TOKEN_NOTVALID'){
this.generateToken();
}else{
this.data = data.result;
resolve(this.data);
}
});
});
}
generateAccessToken(){
var creds = "param1=xxx¶m2=zzz";
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post('myUrlHere', creds, {
headers: headers
})
.map(res => res.json())
.subscribe(
data => {
this.token = data.token;
this.load();
});
}