首页 > 解决方案 > 如何检测电子邮件地址更改是否已恢复?

问题描述

我在我的应用程序的用户个人资料页面上工作,我允许用户更改他们的电子邮件地址。如果用户电子邮件地址更改成功,特定用户的 Firebase 数据库中的数据将被更新。此外,成功更改电子邮件地址后,firebase 会向用户以前的电子邮件地址(更改为新电子邮件地址之前的用户电子邮件地址)发送一封电子邮件,询问是否是更改电子邮件地址的帐户的实际所有者和将有一个链接来重置他们的电子邮件。如果用户选择重置电子邮件(无论出于何种原因),用户的新电子邮件将更改为之前的电子邮件。但问题是数据库中的数据不会被更新,我怎样才能检测到这种变化(电子邮件重置)并更新数据库?

authenticateUserAlert.addAction(UIAlertAction(title: "Done", style: .default, handler: { [weak authenticateUserAlert] (_) in
    // Print the user email
    let emailTextField = authenticateUserAlert?.textFields![0]
    print("Email: \(emailTextField!.text!)")
    // Print the user password
    let passwordTextField = authenticateUserAlert?.textFields![1]
    print("Password: \(passwordTextField!.text!)")

    // Re-authenticate the user
    let user = Auth.auth().currentUser
    let credential = EmailAuthProvider.credential(withEmail: emailTextField!.text!, password: passwordTextField!.text!)

    user?.reauthenticate(with: credential, completion: { (result, error) in
        if error != nil {

            // Alert: What ever the error
            print(error!.localizedDescription)
            Alerts.errorAlert(on: vc, error: error!.localizedDescription, dismissAlert: false)

        } else {
            print(result!)
            let editProfilePage = EditUserProfile()
            editProfilePage.updateUserInfo()
        }
    })
}))

这是我根据答案尝试的

Auth.auth().currentUser?.reload(completion: { (Error) in
    //Completion handler
    if let email = Auth.auth().currentUser?.email {
        UserDataRetrieval.userEmail = email
        self.emailLabel.text = email
        print(Auth.auth().currentUser?.email)
    }
})

将从 firebase 发送给用户的电子邮件 Firebase 数据库

标签: iosswiftfirebasefirebase-authentication

解决方案


这取决于您使用的身份验证类型,但老实说,您应该只使用作为已验证帐户一部分的电子邮件,然后您不必担心在数据库中更新它。

您总是可以通过使用来获取用户的电子邮件Auth.auth().currentUser.email

更新

找到了凭证数据问题的解决方法,尝试使用

Auth.auth().currentUser?.reload(completion: { (Error) in
    if (Error != nil) {
        //Do something with error
    } else {
        //Do something with success or do nothing
    }
})

如果您想始终拥有最新的凭据,只需在应用程序开始时调用更新凭据


推荐阅读