Ad

Firebase AdditionalUserInfo.isNewUser Is Always Return False

I use ionic 4 with firebase/angularfire2 and authenticate with signinwithemeailandpassword() method so I need to check the first time that user login after registration so I found that firebase gives that option with isNewUser

So when creating an account with createUserWithEmailAndPassword() the isNewUser is equal to true as it should or normal! But when login in with signinwithemeailandpassword() always returns false whatever is it the first time to login or not!

This the code for the login method:

 async login(form: NgForm) {

// checking if login is valid
if (form.invalid)
  return this.statusMessage.message('Validation error!');

console.log(this.user);

try {

  const results = await this._afAuth.auth.signInWithEmailAndPassword(this.user.email, this.user.password).then(
    user => console.log(user.additionalUserInfo.isNewUser)

  );
  // console.log(results);

} catch (error) {
  switch (error.code) {
    case "auth/user-not-found":
      this.statusMessage.message("Your email address is wrong! please try again");
      break;

    case "auth/invalid-email":
      this.statusMessage.message("You have enterd invalid email address");
      break;

    case "auth/wrong-password":
      this.statusMessage.message('Password you have enterd is wrong');
      break;

    default:
      console.dir(error);
      this.statusMessage.message('Something wen wrong! please try again later');
      break;
  }

}

the signup code:

async register(form: NgForm) {

    // checking for form validation
    if (form.invalid) return this.statusMessage.message('Validation error!');

    console.log(this.user);

    try {
      await this.afAuth.auth.createUserWithEmailAndPassword(this.user.email, this.user.password).then(
        user => console.log(user)

      );
    } catch (error) {
      this.statusMessage.message('Somthing went wrong!, please try again later.');
    }

  }

}
Ad

Answer

It's false because its only true the first time they sign in. When you call createUserWithEmailAndPassword() the client is logged in as that user. Future signins won't consider the user new anymore.

From the documentation:

On successful creation of the user account, this user will also be signed in to your application.

The only time that isNewUser would be true with a normal login is if you made the account in the Firebase console or using the Firebase Admin SDK.

Ad
source: stackoverflow.com
Ad