Observable passed through Angular 2 service won't fire when subscribed to

185 views Asked by At

I'm building an app with Ionic 2 using firebase as the back end and connecting with angularfire2. I'm trying to get the root page to change when the authentication changes state.

The method in my service that returns the observable looks like this

getUserData() {
  return Observable.create(observer => {
    this.af.auth.subscribe(authData => {
      if (authData) {
        console.log("Auth Changed");
        observer.next(userData);
      } else {
        observer.error();
      }
    });
  });
}

I'm calling the service in my app.conponent file to change the root page

ngOnInit() {
  this.platform.ready().then(() => {
    this.auth.getUserData().subscribe(data => {
    console.log("ngOnit");
      if (!this.isAppInitialized) {
        this.nav.setRoot(HomePage);
        this.isAppInitialized = true;
      }
      this.user = data;
    }, err => {
      this.nav.setRoot(LoginEmailPage);
    });
  });
}

The console reads as below the first time after the page resets

"Auth Changed"
"ngOnit"

but then after another login attempt I only get

"Auth Changed"

The method in the subscription isn't firing.

If I subscribe to another observable it clogs the ngOnit every time so I think it has something to do with the observable I created.

Is there something wrong with the observable I created?

0

There are 0 answers