Firebase数据库和Firebase身份验证中的不同UID [英] Different UID in Firebase Database and in Firebase Auth

查看:80
本文介绍了Firebase数据库和Firebase身份验证中的不同UID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在尝试获取用户数据,但是存在一个巨大的问题.当前登录用户的uid与我数据库中同一用户的UID不同.这是您可以看到的两个图像.图像下方是我在Swift中的SignUpController中的代码.我需要它对于数据库和身份验证是相同的.感谢帮助.

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.

这是第二个

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
        }
    }

推荐答案

我将一些代码移到了正确的位置.另外,您更新了一个名为"uid"的值,但是您无处可参考.我希望下面的代码能正常工作.

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
    }
    }

如果这行得通,则名为uid的变量来自旧帐户.

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

这篇关于Firebase数据库和Firebase身份验证中的不同UID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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