ios - 如何检测电子邮件地址更改是否已恢复?
问题描述
我在我的应用程序的用户个人资料页面上工作,我允许用户更改他们的电子邮件地址。如果用户电子邮件地址更改成功,特定用户的 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)
}
})
解决方案
这取决于您使用的身份验证类型,但老实说,您应该只使用作为已验证帐户一部分的电子邮件,然后您不必担心在数据库中更新它。
您总是可以通过使用来获取用户的电子邮件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
}
})
如果您想始终拥有最新的凭据,只需在应用程序开始时调用更新凭据
推荐阅读
- java - 不确定这个问题需要什么样的算法/什么样的数据结构(打印机队列问题)
- php - 如何将查询分离到另一个类/文件?
- html - 使用 CSS 定位图标和文本
- python - 如何修复 ORA-01036:非法变量名称/编号 DatabaseError
- javascript - 如何摆脱 Javascript 弹出窗口中的“about:blank”标签?
- c++ - PyTorch c++ 扩展中的可选张量
- objective-c - 如何在目标 c 中调用 [[NSBundle mainBundle] appStoreReceiptURL] 时修复内存泄漏?
- java - 为什么这段代码在我的系统上运行良好并在 HackersRank 中抛出 EmptyStackException
- ios - 如何仅使用 GPU 将 CIImage 剪辑到多边形?
- java - 执行需要身份验证的http调用时如何解决循环依赖场景?