我在Firebase身份验证中更改电子邮件地址时遇到一些问题。我的代码现在看起来像这样:
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.
那么,如何更改电子邮件而又不要求用户注销并再次登录?
有一种reauthenticate
专门用于此目的的方法。https://firebase.google.com/docs/auth/ios/manage-users#re-authenticate_a_user
您需要做的是再次询问用户其登录凭据。无需注销-需要登录。
可能的代码:
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
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句