How to use ionViewCanEnter when accessed via URL


#1

In the following code, when transitioning from Page A to Page B, if it is not logged in, redirect to the login page. This worked normally.
However, if accessed via URL to PageB, it will not be redirected to the login page.
It will only display a white page.
How can I redirect to the login page even when accessed via URL?

/** PageA **/
async goToPageB() {
  let isLogin = await this.navCtrl.push(PageB);
  if (!isLogin) {
    this.navCtrl.push(LoginPage);
  }
}

/** PageB **/
async ionViewCanEnter() {
  var user = await firebase.auth().currentUser;
  if (user) { 
    return true;
  } else { 
    // No user is signed in. 
    return false;
  } 
}


#2

yeah, because you need to build up the history-stack :wink:


#3

Thanks,bengtler!

Do you mean that I set defaultHistory: [LoginPage] on PageB as shown in the code below?

/** app.module.ts **/
IonicModule.forRoot(MyApp, {}, {
  links: [
    { component: AddsiyoureiPage, name: 'AddsiyoureiPage', segment: 'addExam', defaultHistory: [LoginPage] },  
  ]
}),

Or is it to write this.navCtrl.push (LoginPage) in PageB as follows?

/** PageA **/
async goToPageB() {
  this.navCtrl.push(PageB);
}

/** PageB **/
async ionViewCanEnter() {
  var user = await firebase.auth().currentUser;
  if (user) { 
    return true;
  } else { 
    // No user is signed in. 
    this.navCtrl.push(LoginPage);
    return false;
  } 
}

If this.navCtrl.push (LoginPage) is described in PageB, when PageB is accessed via URL, PageB is displayed for a moment only, then it transits to LoginPage. When I press the return button in LoginPage, PageB is displayed for a moment, and it transits to LoginPage. In other words, PageB is stacked on the stack.

Sorry for my poor English.