Swift 如何在Firebase中更改电子邮件地址?
我在firebase身份验证中更改电子邮件地址时遇到一些问题。 我的代码现在看起来像这样:Swift 如何在Firebase中更改电子邮件地址?,swift,firebase,firebase-authentication,Swift,Firebase,Firebase Authentication,我在firebase身份验证中更改电子邮件地址时遇到一些问题。 我的代码现在看起来像这样: func changeEmail(withEmail email: String, completion: @escaping ((Bool) -> Void)) { guard let currentUser = Auth.auth().currentUser, let email = mail else { return } currentUser.updateEmail(to:
func changeEmail(withEmail email: String, completion: @escaping ((Bool) -> Void)) {
guard let currentUser = Auth.auth().currentUser, let email = mail else { return }
currentUser.updateEmail(to: email) { [weak self]
error in
guard let self = self else { return }
let title: String
let message: String
if let error = error {
title = "alert.error.title".localized()
message = error.localizedDescription
} else {
title = email
message = "auth.confirm.email.popup".localized()
currentUser.sendEmailVerification()
}
self.navigator.showAlert(title: title,
message: message,
bottomLeftTitle: "general.got.it".localized(),
bottomLeftHandler: { completion(error == nil)
})
}
}
所以它很好用,用户可以更改电子邮件
但当用户停留时间过长,需要重新登录时,问题就会出现。每个人都知道这会干扰应用程序中的用户体验
Auth.auth().reload() //not working in this situation.
那么,如何在不要求用户注销并再次登录的情况下更改电子邮件呢?有一种
重新验证的方法正好用于此目的。
您需要做的是再次询问用户的登录凭据。无需注销-需要登录
可能的代码:
if (self.newPassword == self.newPasswordConfirm) && (!(self.newPassword.isEmpty) || !(self.newUserName.isEmpty)) {
reauthenticate(email: self.accountEmail, password: self.oldPassword) { isSucceeded in
//Successfully authenticated
if isSucceeded == true {
if !self.newUserName.isEmpty {
// update username
}
Auth.auth().currentUser?.updatePassword(to: self.newPassword) { (error) in
// Alert user that it didn't work
}
self.editProfile.toggle()
}
// Failed to reauthenticate
else if isSucceeded == false {
// Alert User
}
}
}