gpt4 book ai didi

ios - 如何在不重新验证的情况下更改 firebase 电子邮件

转载 作者:行者123 更新时间:2023-11-28 07:43:46 27 4
gpt4 key购买 nike

我在我的应用中实现了一个按钮,允许用户使用 Firebase 更改他们的电子邮件。

 @IBAction func resetEmail(_ sender: Any) {
let alertController = UIAlertController(title: "Change Email", message: "", preferredStyle: .alert)
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter New Email Address"

let saveAction = UIAlertAction(title: "Save", style: .default, handler: { (action : UIAlertAction!) -> Void in

//Reset Email

let currentUser = Auth.auth().currentUser
if Auth.auth().currentUser != nil{
currentUser?.updateEmail(to: textField.text!) { error in
if let error = error {
print(error)
} else {
print("CHANGED")
let user = Auth.auth().currentUser
let name = user?.displayName!
let ref = Database.database().reference().child("main").child("users_sen").child(name!).child("email")
ref.setValue(textField.text!)

}
}
}

})


alertController.addAction(saveAction)
}
self.present(alertController, animated: true, completion: {
alertController.view.superview?.isUserInteractionEnabled = true
alertController.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertClose(gesture:))))

})
}

但是,当我运行它并尝试更改电子邮件时,它给我这个错误:

UserInfo={NSLocalizedDescription=This operation is sensitive and requires 
recent authentication. Log in again before retrying this request.

并告诉我重新签名以更改电子邮件。我该如何避免这种情况?如何在不重新登录的情况下更改电子邮件?

这是我更改密码的方式:

  // Password updated.
let currentUser = Auth.auth().currentUser
currentUser?.updatePassword(to: textField.text!) { error in
if let error = error {

} else {
// Password updated.
print("success")

}
}

let userEmail = Auth.auth().currentUser?.email
self.currentPassword = textField.text!

let credential = EmailAuthProvider.credential(withEmail: userEmail!, password: textField.text!)

currentUser?.reauthenticate(with: credential) { error in
if let error = error {
// An error happened.
} else {
// User re-authenticated.
}
}

最佳答案

基于 Firebase 的 documentation ,您需要在执行此类操作时重新验证用户。

Re-authenticate a user Some security-sensitive actions—such as deleting an account, setting a primary email address, and changing a password—require that the user has recently signed in. If you perform one of these actions, and the user signed in too long ago, the action fails with an error. When this happens, re-authenticate the user by getting new sign-in credentials from the user and passing the credentials to reauthenticateWithCredential.

let user = Auth.auth().currentUser
let credential = EmailAuthProvider.credential(withEmail: "email", password: "password")

user?.reauthenticate(with: credential)
{ error in
if let error = error {
// An error happened.
} else {
// User re-authenticated.
user?.updateEmail(to: "newemail")
{ error in

}
}
}

关于ios - 如何在不重新验证的情况下更改 firebase 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51528377/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com