OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在firebase中更新当前用户电子邮件和密码(在用户节点和身份验证中)? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:15
标题: ios - 如何在firebase中更新当前用户电子邮件和密码(在用户节点和身份验证中)?

我正在尝试更新我的用户详细信息页面(用户名、电话号码、电子邮件和密码)。无法更新电子邮件字段,以防它被更新意味着不在两者中(用户和身份验证)。任何 hlp 将不胜感激。 swift2.2中的.plz代码

这里是我的 userDetilsViewController

class UserDetailsViewController: UIViewController {

var user:UserDetails?
let userRegister = UserInfo()
var user_name:String?
var first_name:String?
var last_name:String?
var ph_no:String?
var e_mail:String?
var pass_word:String?
var activeField: UITextField?

@IBOutlet var Username: UITextField!
@IBOutlet var Firstname: UITextField!
@IBOutlet var Lastname: UITextField!
@IBOutlet var email: UITextField!
@IBOutlet var phNo: UITextField!
@IBOutlet var password: UITextField!

var rootRef: FIRDatabaseReference! {
    return FIRDatabase.database().reference()
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)

    let userRef = FIRDatabase.database().reference().child("users")
    userRef.observeEventType(.Value, withBlock: { (snapshot)  in

        for userInfo in snapshot.children {
            self.user = UserDetails(snapshot: userInfo as! FIRDataSnapshot)
            print(self.user)
        }
        if let user = self.user{
            self.Username.text = user.username
            self.Firstname.text = user.firstname
            self.Lastname.text = user.lastname
            self.email.text = user.email
            self.phNo.text = user.phno
        }
    })

    self.navigationController?.popViewControllerAnimated(true)

}
@IBAction func reset(sender: AnyObject) {

    let userRef = FIRDatabase.database().reference().child("users")

    if Username.text == "" {
        Username.text = "enter title"
    } else {
        user_name = Username.text
    }

    if Firstname.text == ""{
        Firstname.text = "Firstname Missing"
    } else {
        first_name = Firstname.text
    }

    if Lastname.text == "" {
        Lastname.text = "Lastname Missing"
    } else {
        last_name = Lastname.text
    }

    if phNo.text == "" {
        phNo.text = "hno Missing"
    } else {
        ph_no = phNo.text
    }

    if email.text == "" {
        email.text = "email Missing"
    } else {
      if let user = FIRAuth.auth()?.currentUser {
          user.updateEmail(email.text!, completion: { (error) in

            if let error = error{
              print(error.localizedDescription)
            } else {
              let alertView = UIAlertView(title: "Update Email", message: "email Updated", delegate: self, cancelButtonTitle: "Ok")
              alertView.show()
            }
         })
         self.e_mail = self.email.text
         print(self.e_mail)
      }
    }

    let updatenote = UserDetails(username: user_name!, firstname: first_name!, lastname: last_name!, email: e_mail!, phno: ph_no!)

    print(updatenote)

    let key = user!.ref!.key
    print(key)

    let updateRef = rootRef.child("/users/\(key)")

    updateRef.updateChildValues(updatenote.toAnyObject())
    navigationController?.popViewControllerAnimated(true)
}



Best Answer-推荐答案


我认为问题出在以下几行:

if let user = FIRAuth.auth()?.currentUser {
     user.updateEmail(email.text!, completion: { (error) in

“用户”是当前用户的副本,而不是对当前用户的引用,因此当您更新用户时,您并没有更新实际的当前用户。

尝试使用此代码更新用户电子邮件地址 - 还要记住,要更新电子邮件,用户必须最近登录。即您必须以用户身份登录才能更改电子邮件。

FIRAuth.auth()?.currentUser?.updateEmail(email.text!) { (error) in
  // ...
}

请注意,我可能完全错了......

关于ios - 如何在firebase中更新当前用户电子邮件和密码(在用户节点和身份验证中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40947075/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4