I have faced a deadlock in an issue while developing in Ionic 3 (angular4). I am simply redoing a formerly written angular app in Ionic but the issue started ever since I decided to use the Ionic "storage" instead of the "localStorage".
The issue is caused by a tiny difference, in local storage the "get" methd simply returns a value, but Ionic's storage "get" method returns a promise.
Now, I need to have a function that reads a specific value from the storage and returns it, and for some reasons I cannot accommodate a promise where I want to use this value.
Is there a way to get a similar behaviour to the localStorage on get method?
getAccessToken()
{
return this._storage.get('bdAccessToken');
}
getAuthorizationOptions(){
let token = this._authService.getAccessToken();
let header = new Headers({
'Authorization': 'Bearer '+ token
});
let ro = new RequestOptions({
headers: header
});
let options = new RequestOptions();
if (options.headers) options.headers.delete("Authorization");
options.headers = header;
return options;
}
get(url:string, options?:RequestOptions):Observable<Response>
{
return super.get(url, this.getAuthorizedOptions())
.catch(err => {
console.log("shit 1");
console.log(err);
if (err && err.status === 401){
console.log("401 here...");
return this._authService.refreshToken()
.flatMap(r =>
super.get(url, this.getAuthorizedOptions())
)
.catch(err2 => {
this.redirect();
return Observable.throw(err2);
});
}
else {
return Observable.throw(err);
}
});
}