I’m trying to use IonViewCanEnter as a guard for one of my app’s pages. The problem is I can’t figure out how to get ionViewCanEnter to wait for a value from an observable. With subscribe I can get the value and print to console, but by then it’s too late and is returning a subscription.
I assumed some similarity to Angular 2’s CanActivate in that I need to return a completed Observable, but solutions that I’ve found that use map with .take(1) or first() don’t seem to fire at all.
From my page:
ionViewCanEnter() {
return this._auth.checkToken()
.map(
res => {
console.log(res);
return false;
},
err => {
console.log(err);
}
)
.take(1)
// .subscribe(
// res => {
// console.log(res);
// return res;
// },
// err => {
// console.error(err);
// }
// );
}
From my _auth
provider:
checkToken() {
return Observable
.fromPromise(this.defaultOptions())
.switchMap(
options => {
return this.http.get(Config.API_ENDPOINT + '/api/', options)
.map(res => res.json())
.map(
res => {
if (res.auth_token) {
this.setToken(res.auth_token, undefined);
return true;
}
// this.logout();
return false;
},
err => {
return false;
// this.logout();
}
)
}
);
}