firebase.auth().currentUser shows me null but it logged in

hey,
i’m trying to display the current user but it shows me null.
this is my code

firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.rootPage = HomePage;
console.log(“I’m here! HomePage”);
} else {
this.rootPage = LoginPage;
console.log(“I’m here! LoginPage”);
}
});

after login it goes to HomePage and it working good.
then i wrote

this.currentUser = firebase.auth().currentUser;
console.log(this.currentUser);`

it print me null.

Any news on this?

I’m facing the same issue and get crazy about it.

trying console.log(firebase.auth()) shows me the whole object but console.log(firebase.auth().currentUser) is null

Found it myself :wink:


during initialization firebase.auth().currentUser is null so you should use

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

as recommended here https://firebase.google.com/docs/auth/web/manage-users

1 Like

i solve it.
check it

import { Component, ViewChild } from ‘@angular/core’;
import { Platform, Nav, LoadingController } from ‘ionic-angular’;
import { StatusBar, Splashscreen } from ‘ionic-native’;

//import pages
import { TabsPage } from ‘…/pages/tabs/tabs’;
import {HomePage} from “…/pages/home/home”;
import {LoginPage} from “…/pages/login-page/login-page”;
import firebase from ‘firebase’;

@Component({
template: ‘<ion-nav [root]=“rootPage”>’,
})
export class MyApp {

@ViewChild(Nav) nav: Nav;
public rootPage:any;

constructor(
platform: Platform,
public loadingCtrl: LoadingController
) {
firebase.initializeApp({
apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
authDomain: "xxxxxxxxxxxxxxxxxxxxxxxxxx"
databaseURL: "xxxxxxxxxxxxxxxxxxxxxxx"
storageBucket: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
messagingSenderId: “5xxxxxxxxxxxxxxxxxx”
});

let loader = this.loadingCtrl.create();
loader.present();
this.listenToUserStatusUpdate(loader);
let fireBaseUser = firebase.auth().currentUser;
console.log(fireBaseUser);
this.rootPage = fireBaseUser ? TabsPage : LoginPage;
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();
});

}

listenToUserStatusUpdate(loader: any) {
firebase.auth().onAuthStateChanged((user) => {
if(loader)
loader.dismiss();
console.log(“The User:”, user);
if (user) {
this.nav.setRoot(TabsPage);
} else {
this.nav.setRoot(LoginPage);
}
});
}
}

1 Like