firebase.auth().currentUser never works?

19,105 views
Skip to first unread message

Jason Sun

unread,
May 25, 2016, 9:57:03 PM5/25/16
to Firebase Google Group
  var user1;
  firebase
.auth().onAuthStateChanged(function(user) {

   
if (user) {
      user1
= user;
      console
.log("user logged in");
      console
.log("checkpoint");
      console
.log(user.uid);   // this prints fine
     
     
var num;
      firebase
.database().ref('users/'+user.uid).once('value').then(function(snapshot){
        num
= snapshot.numChildren();
        console
.log(num);
     
});


     console
.log(user1.uid);

   
}
   
else{
      console
.log("user not logged in");
   
}
 
});

console.log(user1.uid) // this goes to null, even when i put it in a while loop

var user = firebase.auth().currentUser //this goes to null always too

 


I can't seem to ever get firebase.auth().currentUser working. It always returns null. Any help?

Dennis Jefferson

unread,
May 26, 2016, 10:19:56 AM5/26/16
to Firebase Google Group
Hey Jason,

Checking firebase.auth().currentUser during firebase initialization will return null. Try setting an observer...and checking the value of firebase.auth().currentUser after the observer is fired.

From the docs:


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

Jason Sun

unread,
May 26, 2016, 10:30:20 AM5/26/16
to Firebase Google Group
Hey Dennis,

Thanks for the response. 

As I understand it, the observer will only be triggered when there is a change in the Auth state or after initialization. I've tried setting multiple observers but only the first one seems to run

Jason

boj...@google.com

unread,
May 26, 2016, 9:03:20 PM5/26/16
to Firebase Google Group
A newly attached observer should trigger as soon as the initial state is resolved (null for logged out and the currentUser for logged in). It will then only trigger when there is an auth state change (new log in, log out, etc.).
Every time you attach a new observer, it will follow the same behavior, trigger first on initial state resolution and then anytime the auth state changes.

Thadeu Luz

unread,
Jun 6, 2016, 10:52:52 PM6/6/16
to Firebase Google Group
Is there a way to get a promise that resolves when auth is initialized? Or something like "once" for onAuthStateChanged()?

boj...@google.com

unread,
Jun 6, 2016, 11:21:07 PM6/6/16
to Firebase Google Group
You can unsubscribe your observer after the first call:
var observer = function(user) {
  // First call, do your thing.
  unsubscribe();
};
var unsubscribe = auth.onAuthStateChanged(observer);

You can also package the above logic in a promise. Something like this:

var p = new Promise(function(resolve, reject) {
  var observer = function(user) {
    // Unsubscribe on first call.
    unsubscribe();
    // Resolve with current state.
    resolve(user);
  };
  var unsubscribe = auth.onAuthStateChanged(observer);
});

Thadeu Luz

unread,
Jun 7, 2016, 11:26:43 AM6/7/16
to Firebase Google Group
Great. Thanks. 
Reply all
Reply to author
Forward
0 new messages