Different UID in Firebase Database and in Firebase Auth

做~自己de王妃 提交于 2019-12-11 04:57:47

问题


Currently I am trying to fetch user data, but there is one huge problem. The uid of currently logged in user is different then UID of the same user in my database. Here are two images where you can see it. Below the images is my code from my SignUpController in Swift. I need it to be the same for the database and auth. Thanks for help.

And here is second one

func emailSingUp(){
    guard let email = emailTextField.text, let password = passwordTextField.text,let name = nameTextField.text else {
        print("unsuccessful signup")
        return
    }
    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
        if error != nil{
            print(error!)
            return
        }
    }
    guard let uid = Auth.auth().currentUser?.uid else{
        print("There is no UID to access")
        return
    }

    let ref = Database.database().reference(fromURL: "https://yourapp.firebaseio.com/")
    let userReference = ref.child("users").child(uid)

    let values = ["name":name, "email":email]
    userReference.updateChildValues(values) { (err, ref) in
        if err != nil{
            print(err!)
            return
        }
    }

回答1:


I moved some code into the right spot. Also, you update a value called "uid" but you nowhere have a reference to that. I hope the code below works.

func emailSingUp(){
    guard let email = emailTextField.text, let password = passwordTextField.text,let name = nameTextField.text else {
        print("unsuccessful signup")
        return
    }
    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
        if error != nil{
            print(error!)
            return
        }else{
let values = ["name":name, "email":email]
Database.database().reference().child("users").child("\(Auth.auth().currentUser!.uid)").updateChildValues(values) { (err, ref) in
    if err != nil{
        print(err!)
        return
    }
    }

If this works, you variable called uid is from an old account.



来源:https://stackoverflow.com/questions/44833358/different-uid-in-firebase-database-and-in-firebase-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!