【问题标题】:Firebase additionalUserInfo.isNewUser is always return falseFirebase additionalUserInfo.isNewUser 总是返回 false
【发布时间】:2019-07-17 10:48:15
【问题描述】:

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

  }

}

【问题讨论】:

  • 请多一些代码
  • @ShashankVivek 你还需要什么?我用我也用过的注册方法更新了问题!
  • user.additionalUserInfo.isNewUser 的响应总是在第一次登录后返回 false,这是我的问题!
  • firebaser here 据我所知the isNewUser value comes straight from the server,所以应该不可能在那里得到错误的值。你能在像 JSBin 这样的网站上设置一个快速、最小化的重现(最好只使用 JavaScript SDK,不要使用其他框架),以便我可以与团队分享吗?
  • @FrankvanPuffelen 据我了解,createUserWithEmailAndPassword() 方法在我调用它后也已登录用户!所以未来的签约不会再认为用户是新用户。这对我来说很奇怪!为了使用 isNewUser 我必须在注册后将用户重定向到他的个人资料!无需再次手动登录!

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


【解决方案1】:

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

来自documentation

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

isNewUser 唯一在正常登录时为 true 的情况是您在 Firebase 控制台中创建帐户或使用 Firebase Admin SDK。

【讨论】:

  • 那么如何避免这种情况!我需要让用户注册,然后在他登录时重定向到登录页面,如果它是第一次登录其他主页,则重定向到个人资料页面!
  • @heshamshawky 只需在他们创建帐户时重定向他们,他们已经登录,无需在他们刚注册后让他们登录。
  • 很好的答案迈克尔!我完全错过了问题出在随后的登录上。 @heshamshawky:处理的典型方法是在用户登录后检测您是否有完整的用户个人资料。如果您没有完整的个人资料,则将他们重定向到“完成您的个人资料”页面,否则重定向到你的主要活动。
猜你喜欢
  • 2016-12-20
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-20
  • 2021-11-05
  • 2018-11-05
  • 2019-05-06
相关资源
最近更新 更多