# ionViewCanEnter() doesn't work

**URL:** <https://forum.ionicframework.com/t/ionviewcanenter-doesnt-work/128541>\
**Category:** ionic-v3\
**Created:** [April 22, 2018, 12:24am UTC](https://forum.ionicframework.com/t/ionviewcanenter-doesnt-work/128541 "2018-04-22T00:24:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nakamurakota](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nakamurakota/32/118940_2.png) [@nakamurakota](https://forum.ionicframework.com/u/nakamurakota)\
**Post date:** [April 22, 2018, 12:24am UTC](https://forum.ionicframework.com/t/ionviewcanenter-doesnt-work/128541/1 "2018-04-22T00:24:40Z")

</div>

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.

```auto
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;
      }
   }
}

```

---

<div class="post-metadata">

**Author:** ![robinyo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/robinyo/32/115964_2.png) [@robinyo](https://forum.ionicframework.com/u/robinyo)\
**Post date:** [April 22, 2018, 1:52am UTC](https://forum.ionicframework.com/t/ionviewcanenter-doesnt-work/128541/2 "2018-04-22T01:52:45Z")

</div>

Try:

```auto
ionViewCanEnter() {

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

    return this.authService.authenticated;
 }

```

See:

- [https://github.com/ionic-team/ionic/issues/12193#issuecomment-363177872](https://github.com/ionic-team/ionic/issues/12193#issuecomment-363177872)
- [NavGuards with ionic-angular 3.5.0 has a breaking change](https://forum.ionicframework.com/t/navguards-with-ionic-angular-3-5-0-has-a-breaking-change/96863/7)

---

<div class="post-metadata">

**Author:** ![nakamurakota](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nakamurakota/32/118940_2.png) [@nakamurakota](https://forum.ionicframework.com/u/nakamurakota)\
**Post date:** [April 25, 2018, 3:07pm UTC](https://forum.ionicframework.com/t/ionviewcanenter-doesnt-work/128541/3 "2018-04-25T15:07:08Z")

</div>

Thanks, robinyo

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

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

```

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:38pm UTC](https://forum.ionicframework.com/t/ionviewcanenter-doesnt-work/128541/4 "2021-07-19T21:38:58Z")

</div>

Closed on July 19, 2021, 9:38pm UTC


