Firebase AdditionalUserInfo.isNewUser始终返回false

时间:2019-02-23 13:58:55

标签: typescript firebase ionic-framework firebase-authentication angularfire2

我将ionic 4与firebase / angularfire2结合使用,并通过signinwithemeailandpassword()方法进行身份验证,因此我需要在注册后检查用户首次登录,因此我发现firebase在isNewUser中提供了该选项

因此,在使用createUserWithEmailAndPassword()创建帐户时,isNewUser应该是equaltrue,这应该是正常的!但是,无论第一次登录与否,使用signinwithemeailandpassword()登录总是返回false!

这是登录方法的代码:

 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;
  }

}

注册代码:

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.');
    }

  }

}

1 个答案:

答案 0 :(得分:3)

这是错误的,因为只有在他们第一次登录时才是真的。当您致电createUserWithEmailAndPassword()时,客户端以该用户身份登录。将来的登录将不再考虑该用户是新用户。

来自documentation

  

成功创建用户帐户后,该用户也将登录到您的应用程序。

只有在您使用Firebase控制台或使用Firebase Admin SDK进行帐户登录的情况下,isNewUser才能正常登录。