【问题标题】:Firebase/javascript -- How to connect data created in firestore with userFirebase/javascript -- 如何将在 Firestore 中创建的数据与用户连接
【发布时间】:2021-11-02 10:33:00
【问题描述】:

Firebase v9

如何将在 Firestore 中创建的数据与创建此数据的用户联系起来?

我使用createUserWithEmailAndPassword 在firebase中授权用户的功能,授权后我使用onAuthStateChanged获取用户数据,但我如何将特定用户与仅由他们创建的数据连接起来?

【问题讨论】:

    标签: javascript reactjs firebase firebase-authentication


    【解决方案1】:

    例如,要为特定用户创建个人资料文档,常见的模式是使用用户的 UID 作为文档 ID。像这样的:

    import { getAuth, onAuthStateChanged } from "firebase/auth";
    import { doc, setDoc } from "firebase/firestore"; 
    
    const auth = getAuth();
    onAuthStateChanged(auth, (user) => {
      if (user) {
        const uid = user.uid;
    
        await setDoc(doc(db, "users", uid), {
          name: user.displayName
        });
        // ...
      }
    });
    

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 2016-10-08
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      相关资源
      最近更新 更多