ios - 如何检测电子邮件地址更改是否已还原?

标签 ios swift firebase firebase-authentication

我在我的应用程序的用户个人资料页面上工作,我允许用户更改他们的电子邮件地址。如果用户电子邮件地址更改成功,则特定用户的 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)
    }
})

The email that will be sent to the user from firebase Firebase datbase

最佳答案

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

您始终可以使用 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
    }
})

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

关于ios - 如何检测电子邮件地址更改是否已还原?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57282828/

相关文章:

ios - 在运行时在 QuickDialog iOS 中将 QSection 插入到 QRootElement

ios - Google Cloud 功能中仅接收部分 Apple 订阅通知

ios - 刷新 token 如何在 OAuth 流程中工作?

ios - 使用 NSFetchedResultsController 在底部 UITableView 中加载更多内容

ios - 为什么 UIButton.layer.maskedCorners 接受分配显式数组而不是数组变量?

android - 如何将事件参数记录到 Firebase 控制台

java - 如何将 long 或 double int 数据类型转换为 Ffirebase dataSnapshot 中的 String

ios - SwiftUI Charts BarMark 将注释与图表顶部对齐

ios - UIScrollView: View 不会自动滚动

android - 如何将 repo 注入(inject) FirebaseMessagingService 类