【问题标题】:How to update Email Address in Firebase Authentication in Swift 4如何在 Swift 4 中更新 Firebase 身份验证中的电子邮件地址
【发布时间】:2019-03-02 11:31:36
【问题描述】:

我只想更新当前用户的身份验证电子邮件地址。我已经尝试了很多解决方案,比如 firebase 的 updateEmail 方法,但它不起作用!如果有人知道,请告诉我如何实现这一点提前谢谢!!

@IBAction func btnResetEmailClick(_ sender: UIButton) {
        let auth = Auth.auth()
        guard let email = self.txtEmailAddress.text ?? auth.currentUser?.email else { return }
        // email that i have to update with current user email
        auth.currentUser?.updateEmail(to: (auth.currentUser?.email)!, completion: { (error) in
            if error == nil{

            }else{
            }
        })
    }

【问题讨论】:

  • 您分享的代码有什么问题?如:当您在调试器中运行该代码时,该代码中的哪一行不符合您的预期?
  • 是的,我调试代码!它将进入 if 循环意味着它成功,现在我应该做什么!
  • 您似乎正在将电子邮件地址更新为相同的现有电子邮件地址。即将(to: (auth.currentUser?.email)! 更改为(to: (email)!。例如,请参阅我对this 问题的回答。
  • 好的,谢谢,我会检查并告诉你:)

标签: ios firebase firebase-authentication swift4


【解决方案1】:

要更改用户最近必须登录的电子邮件地址,我建议这样做:

var credential: AuthCredential

@IBAction func changeEmail() {
    if let user = Auth.auth().currentUser {
        // re authenticate the user
        user.reauthenticate(with: credential) { error in
            if let error = error {
                // An error happened.
            } else {
                // User re-authenticated.
                user.updateEmail(to: "email") { (error) in
                    // email updated
                }
            }
        }
    }
}

【讨论】:

  • 当凭证未初始化时,user.reauthenticate(with: credential) 将崩溃。
【解决方案2】:

这是解决问题的有效方法。

let user = Auth.auth().currentUser
user?.updateEmail(to: "email") { error in
if error != nil {
    // An error happened
} else {
   // Email updated.
   }
}

【讨论】:

  • 多次enter code here 是什么意思?你的答案有点错误。
猜你喜欢
  • 1970-01-01
  • 2020-11-22
  • 2021-07-12
  • 2019-11-25
  • 1970-01-01
  • 2023-03-18
  • 2018-11-11
  • 2018-02-10
  • 2017-10-03
相关资源
最近更新 更多