forvaidya
forvaidya

Reputation: 3315

Auth object Null despite successful a auth

I have following code in index.js and (necessary config = {...}) object I am running this as node index.js and my Auth object Null despite a successful auth

app = firebase.initializeApp(config);
fstore = firebase.firestore()

collection = fstore.collection('issue');



auth = firebase.auth().signInWithEmailAndPassword("[email protected]", "secretPassword")
  .then((creds) =>  {
    console.log(creds.user.uid) ;
}) ;

Despite above call successful - Auth object is not available

var user = app.auth().currentUser ;
console.log("Try - current user")
console.log(user) ;
console.log("DONE try  - current user")

Upvotes: 0

Views: 39

Answers (1)

Andrii Rudavko
Andrii Rudavko

Reputation: 370

Auth object might not yet be available when your second code snippet executes. Try adding authenticated state observer to know when the auth state changes, and have access to the user object:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    var uid = user.uid;

  }
});

This code should be placed before actual authentication.

Upvotes: 2

Related Questions