将电子邮件更新为Firebase [英] Updating Email to firebase

查看:69
本文介绍了将电子邮件更新为Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它在我的控制台中打印CHANGED,但没有任何变化,并且用户电子邮件保持不变.占位符文本将还原为原始电子邮件,并且该特定用户的电子邮件的Firebase数据库也保持不变.我希望用户可以选择更改其个人资料的任何部分,包括首次注册时存储的电子邮件,密码,位置和其他值.为此,我必须注销他们并让他们重新登录吗?这似乎是糟糕的用户体验.

It prints CHANGED in my console but nothing is changing and the users email is remaining the same. The placeholder texts reverts back to the original email and my Firebase Database for email for that specific user is also unchanged. I want to allow the user to have the option to change any part of their profile including email, password, location, and other values they stored when first signing up. In order to do this will I have to log them out and have them resign in? That would seem to be a terrible user experience.

class SettingsViewController: UIViewController {

@IBOutlet weak var email: UITextField!
@IBOutlet weak var password: UITextField!
@IBOutlet weak var location: UITextField!
@IBOutlet weak var updateEmail: UIButton!
@IBOutlet weak var updateLocation: UIButton!
@IBAction func updateEmailAction(_ sender: Any) {

    let currentUser = Auth.auth().currentUser

    currentUser?.updateEmail(to: email.text!) { error in
        if let error = error {
            print(error)  
        } else {
            print("CHANGED") 
        }
    }
}

var dataBaseRef: DatabaseReference! {
    return Database.database().reference()
}

override func viewDidLoad() {
    super.viewDidLoad()

    let userRef = dataBaseRef.child("users/\(Auth.auth().currentUser!.uid)")

    userRef.observe(.value, with: { (snapshot) in

        let user = Users(snapshot: snapshot)

        if let email = user.email{
            self.email.placeholder = email  
        }

        if let location = user.location{
            self.location.placeholder = location
        }
    }  
)}

推荐答案

.updateEmail函数更新其 Firebase凭据中的用户电子邮件,与/users节点中存储的内容无关.

The .updateEmail function updates the users email in their Firebase credentials and has nothing to do with what's stored in your /users node.

如果您在此处保留重复的电子邮件,则需要在.updateEmail函数成功后将数据写入/users/uid节点.即/users节点是您创建的 ,不是由Firebase维护的.

If you are keeping a duplicate email there, you would need to write that data to the /users/uid node when the .updateEmail function succeeds. i.e. the /users node is something you created, and is not maintained by Firebase.

@IBAction func updateEmailAction(_ sender: Any) {

   let currentUser = Auth.auth().currentUser

   currentUser?.updateEmail(to: email.text!) { error in
       if let error = error {
           print(error)  
       } else {
           print("CHANGED")
           let uid = Auth.auth().currentUser!.uid
           let thisUserRef = fbRef.child("users").child(uid)
           let thisUserEmailRef = thisUserRef.child("email")
           thisUserEmailRef.setValue(email.text!) 
       }
   }
}

这篇关于将电子邮件更新为Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆