Ionic 2 and Firebase auth issue


#1

Well, I’m testing a app with firebase, and I using a auth by Email, but I’m using firebase.auth() to look if the user is already logged in, the code looks to be right for me, but the rootPage does not recive nothing! Someone can help me?

import { Component, ViewChild } from '@angular/core';
import { Platform, NavController, MenuController } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';

import { HomePage } from '../pages/home/home';
import { Login } from '../pages/login/login';
import { Clientes } from '../pages/clientes/clientes';
import firebase from 'firebase';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild('conteudo') nav : NavController;
  public pages: any;
  public clientes: any;
  private rootPage: any;

  constructor(platform: Platform, private menu:MenuController) {

    this.menu = menu;
    this.pages = [
        { title: 'Home', component: HomePage, icon: "home"},
    ];
    this.clientes = [
        { title: 'Clientes', component: Clientes, icon: "md-contact"},
        { title: 'Banco de Horas', component: HomePage, icon: "ios-timer"},
        { title: 'Reservas', component: HomePage, icon: "calendar"},
    ];

    var config ={
      apiKey: "<MY API KEY>",
      authDomain: "<MY DOMAIN>",
      databaseURL: "<MY DATABASE URL>",
      storageBucket: "< MY STORAGE BUCKET>"
    }

    firebase.initializeApp(config);

    firebase.auth().onAuthStateChanged((user) => {
      if (user) {
        this.rootPage = HomePage;
      } else {
        this.rootPage = Login;
      }
    });

    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();
      Splashscreen.hide();
    });
  }

  openPage(page) {
    this.menu.close();
    this.nav.setRoot(page.component);
  };
}

#2

@isaacdart When you say it does not receive anything, do you mean that it doesnt get the login details? you need to pass data to your pages using navParams e.g.

firebase.auth().onAuthStateChanged((user) => {
      if (null === user) {
        this.nav.setRoot(HomePage, {'user': user});
      } else {
        this.nav.setRoot(Login);
      }
    });

In your HomePage you can retrieve the NavParams as such

    // Import NavParams if needed
    import {NavParams} from 'ionic-angular';

        private userData; any;

        constructor(private navCtrl: NavController,
            private navParams: NavParams) {
        }

        ionViewDidLoad () {
             this.userData = this.navParams.get('user');
             console.log(this.userData);
        }

Alternatively you could just import firebase in your HomePage and use it directly

import firebase from 'firebase';

ionViewDidLoad() {
    console.log(firebase.auth().currentUser);
}