ionViewCanEnter doesn't work as expected


#1

Hello,

I’m using ionic 3.20

I think ionViewCanEnter doesn’t work as expected. My assumption was that the view won’t be rendered if it returns false. What I see is that the view is rendered en displayed for a short time (0.5 second?), then I’m redirected to the login page. Preferably I like the view not being rendered if it returns false.

I implemented it like this:

  ionViewCanEnter() {
    return this.auth.authenticated(this.navCtrl);
  }

And auth.authenticated is implemented like this:

authenticated(nav: NavController): boolean | Promise<any> {
    return this.storage.get("token").then(token => {
      console.log('from storage: ' + token);
      if (token == null) {
        setTimeout(() => {
          nav.setRoot("SignInPage")
        }, 0);
      } else {
        return true;
      }
    }).catch(() => {
      setTimeout(() => {
        nav.setRoot("SignInPage")
      }, 0);
      return false;
    });
  }

Please let me know how I can improve this.
Roy


#2

See:


ionViewCanEnter didn't work after pop()
#3

Ok, but is there a workaround? Or how can I improve thanks?

Or do you suggest another temporary solution ?


#4

Is it really the purposes of the ionViewCanEnter function to actually change the navstack?

I’d say it should only return true or false, so the pushing page can use a catch to figure out what to do.


#5

Ok maybe, but how does that solve the issue that the page gets rendered for a while and then redirects to the SignInPage


#6

Well, not sure if that is relevant in the case of the use I am referring to as then Ionic will refuse to allow the navigation to pass and hence the page to render at all (according to spec)

Now you are actually not resolving to true or false, using storage to retrieve data and a timeout (of 0) to eventually set the nav. These steps may take a bit.