【问题标题】:How to generate same user id on firebase real time database and authentication如何在firebase实时数据库和身份验证上生成相同的用户ID
【发布时间】:2025-11-30 08:55:01
【问题描述】:

我正在尝试在 React Native 中创建登录注册屏幕,并希望获取使用注册/登录注册的用户的详细信息。到目前为止,详细信息已存储在其中,但数据库中的用户 ID 和身份验证在 Firebase 中是不同的,以便获取它们。如果两个表中的用户 ID 不同,如何从 firebase 获取用户详细信息。

userLogin = (email, pass) => {
        firebase.auth().signInWithEmailAndPassword(email, pass)
        .then(() => {
            navigation.navigate("HomeScreen")
        })
        .catch(err => {
            Alert.alert(err.message)
        })
    }

【问题讨论】:

    标签: reactjs firebase react-native firebase-realtime-database firebase-authentication


    【解决方案1】:

    当您使用以下代码登录时:

    firebase.auth().signInWithEmailAndPassword(email, password)
      .then((res) => {
        console.log(res.user.uid);
      })
      .catch((error) => {
        var errorCode = error.code;
        var errorMessage = error.message;
      });
    

    在这里您可以从 firebase 身份验证中获取 uid,然后您可以在 firebase 实时数据库中使用它:

    var database = firebase.database();
     database.ref('users/' + userId).set({
        username: name,
        email: email,
        profile_picture : imageUrl
      });
    

    【讨论】:

    • 我是 React-Native 的新手,你建议的那个没有用,或者可能是我做错了。你能再发一个吗,我已经用我的登录代码更新了我的问题。
    • 什么没用,你做了console.log(res.user.uid);吗?
    • 哦,对不起,我使用了 userId。不过,当我使用 res.user.uid 时,它确实有效,谢谢。