【问题标题】:How can you add a user phone number in the same child as email?如何在同一个孩子中添加用户电话号码作为电子邮件?
【发布时间】:2021-12-19 09:18:02
【问题描述】:

用例:围绕电子邮件构建应用程序(此处包含三个重要子项)。现在我只希望手机登录用户拥有相同的孩子。 所以下面这样的事情一定不会产生错误(它目前确实如此)。我可以通过电话登录做其他所有事情(甚至创建一个带有电话号码的用户),但我希望他也有这些孩子,因为当用户使用电话而不是电子邮件时。

Auth.auth().createUser(withEmail: ResultString, password: remainingPart) { (user, error) in
  let databaseRef = Database.database().reference()
  guard error == nil else { return }
  guard let user = user else { return }
  let userObject =
    [
      "users": ResultString,
      "postID": user.user.uid,
      "e2": remainingPart,
    ] as [String: Any]

  databaseRef.child("people").child(user.user.uid).setValue(userObject)
  print("YESSSSS")
}

【问题讨论】:

  • 您共享的代码的哪一部分没有按照您的预期执行?
  • YESSSSS 永远不会被打印出来。 Auth.auth().createUser(withEmail: ResultString, password: remainingPart) { (user, error) 将其视为错误,因为 ResultString 是电话号码而不是电子邮件,remainingPart 是电话号码的最后 4 位数字,而不是实际密码。我想虽然问题是电子邮件是电话号码并且没有@
  • 如果createUser(withEmail:, password:) 中有错误,那么error 变量会告诉您出了什么问题。我建议检查该值。
  • 我试过了,它打印出来:Error Domain=FIRAuthErrorDomain Code=17008 "The email address is badly formatted." UserInfo={NSLocalizedDescription=The email address is badly formatted., FIRAuthErrorUserInfoNameKey=ERROR_INVALID_EMAIL} error . 所以我认为电话号码不会被接受为电子邮件

标签: swift firebase firebase-realtime-database firebase-authentication


【解决方案1】:

您似乎正在尝试将电话号码传递给createUser(withEmail:,password:)。由于电话号码不是有效的电子邮件地址,API 会拒绝它。

要使用用户的电话号码登录,请遵循phone number sign-in 的文档。

在向用户输入电话号码后,您仍然可以将他们的详细信息写入数据库,就像您现在所做的一样。

【讨论】:

  • 谢谢,这是有道理的。但是在这种情况下你将如何处理 uid,即假设用户已经用电话号码创建并转到下一页
  • 我不明白你的意思。你能澄清一下吗?
  • 我的意思是,写入数据库时​​会是什么样子?那么在databaseRef.child("people").child(user.user.uid).setValue(userObject) 中,user.user.uid 部分你会做什么?
  • 如果用户已登录,则为Auth.auth().currentUser.uid。见firebase.google.com/docs/auth/ios/manage-users
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
  • 2019-10-20
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多