ionViewCanEnter() doesn't work


#1

ionViewCanEnter() doesn’t work.
I use ionic version 3.20.0.
Please forgive me that my English is not good.

I want to enter PageB only when logged in. The following code is based on ionic’s official document. I check the login status and login function using firebase. When I executed the following code, I put it in PageB whether it is in login status or logout status. Is my way of using ionViewCanEnter wrong?

What I would like to implement is to switch to PageB from Page A, enter PageB if logged in, and transition to login page if logged out.

export class PageA{
 constructor(
   public navCtrl: NavController
  ){}

  pushPage(){
    this.navCtrl.push(PageB);
  }

}

export class PageB(){
  constructor(
    public navCtrl: NavController
  ){}
  ionViewCanEnter(): boolean{
   firebase.auth().onAuthStateChanged(function(user) {
     if(user){
       // login
       return true;
      } else {
       // logout
       return false;
      }
   }
}

#2

Try:

ionViewCanEnter() {

    if (! this.authService.authenticated) {
        setTimeout(() => this.navCtrl.setRoot('login'))
    }

    return this.authService.authenticated;
 }

See:


#3

Thanks, robinyo

I succeeded after executing the following source code. I ran successfully the source code below.

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