Firebase 4.1.3: onAuthStateChange not called after first login with email and password

I upgraded the Firebase version for my app from 3.5.0 to 4.1.3 and noticed that the onAuthStateChange callback function is no longer called after the user successfully subscribes for the first time after verifying their email address.

The application is written in JavaScript.

These are the relevant sections of my code:

Callback setup

firebase.auth().onAuthStateChanged(onAuthStateChange);

      

Callback

function onAuthStateChange (user) {
    console.log(user); // Not appearing in console
}

      

To come in

firebase.auth().signInWithEmailAndPassword(email, password)
    .then(function (user) {
        console.log("signInWithEmailAndPassword success"); // This appears in the console
    })
    .catch(function(error) {
        console.log("signInWithEmailAndPassword", error);
        self.signInError = error.message;
        $timeout(function () {
            $scope.$apply();
        });
    });

      

Edit - these are the steps to reproduce the issue (typical user action of my application):

  • Downloading and running applications
  • User logs in with email and password
  • The app sends email by email.
  • The user receives a confirmation email and clicks on the link
  • User navigates to app login page
  • User signs up when running console.log ("signInWithEmailAndPassword success");
  • onAuthStateChanged callback is not called

For development and testing purposes (not what the user will do, but I did)

  • The user reloads the application.
  • The user is now in the application
  • The user signs the application.
  • The user logs into the application running console.log ("signInWithEmailAndPassword success");
  • onAuthStateChanged callback called
+3


source to share


2 answers


Try this way, I hope it will work



firebase.auth().onAuthStateChanged(function(user){
console.log(user);
})

      

0


source


The problem is that auth (). CreateUserWithEmailAndPassword () actually registers the user type to.

You will find that if you enter

firebase.auth().onAuthStateChanged(user => {
  console.log("onAuthStateChanged()"); 

      

that createUserWithEmailAndPassword () actually starts the console log. However, there doesn't seem to be a valid "user" object to explain why nothing appears for you, since you are only registering a user.

I faced exactly the same problems. In the sendEmailverification () step, notice how it requires the use of auth (). CurrentUser, signaling that there should be some kind of user logged in (I'm not sure how firebase handles the difference between verified email users and unverified users behind scenes)



You can simply call the signOut () function after sending the confirmation email, and that should allow the onAuthStateChanged () function to be called on the first login.

  firebase.auth().currentUser.sendEmailVerification()
  .then(() => {
    console.log("Email verification sent");
    firebase.auth().signOut().then(() => {
      console.log("Signed out!");
    }).catch((err) => {
      console.log("Error signing out!", err);
    });

      

The odd thing is that you can actually successfully log in without causing changes to AuthStateChanged or returning any errors.

TL; DR: don't forget to use auth () function. SignOut () after sending confirmation email.

0


source







All Articles