代码之家  ›  专栏  ›  技术社区  ›  Guillermo Moratorio

要求授权。在云Firestore规则中,uid似乎总是空的

  •  7
  • Guillermo Moratorio  · 技术社区  · 7 年前

    我一定错过了一些非常基本的东西。。。但每当我给我的云Firestore db打电话,试图制定任何安全规则时,它们总是失败。

    像这样做

        match /users/{userId} {
          allow create, read, update: if true;
        }

    这是可行的,但显然违背了这一点。然而,如果我做任何类型的额外审查,比如所有文档中的go-to示例

        match /users/{userId} {
          allow create, read, update: if request.auth.uid != null;
        }

    每次都会失败。在如何将客户端代码连接在一起时,我是否遗漏了一些明显的东西?

    这是我的客户端代码,它让用户登录,然后调用db来获取用户。 (请注意,在my db中,用户的密钥是通过电子邮件,而不是uid)

    // this is a snippet from the code where I log the user in
    firebase.auth().signInWithEmailAndPassword(email, FIREBASE_USER_PASSWORD)
                .then(user => {
                // the user comes back successfully logged in, 
                // I grab its uid and add it to a preparedUser object that I've been building, 
                // then pass this user to my getFirestoreUserObject function
                    preparedUser.uid = user.uid;
                    getFirestoreUserObject({dispatch, user: preparedUser, navigate});
                })
                
    // then the getFirestoreUserObject function:
    // note that all the code below works fine when there are no security rules in place
    
    const getFirestoreUserObject = ({dispatch, user, navigate}) => {
        const {name, email, imageUrl} = user;
        // if I ask for currentUser here, I get back my logged in user
        const currentUser = firebase.auth().currentUser;
    
        // email is defined correctly here as the user's email
        firebase.firestore().collection('users').doc(`${email}`)
            .get() // the request fails here due to insufficient permissions
            .then(doc => {
                if (doc.exists) {
                    const currentUser = doc.data();
    
                    getUserFavorites({dispatch, currentUser, navigate});
                } else {
                    createUserInFirestore({dispatch, user, navigate});
                }
            })
    };

    有什么明显的东西我遗漏了吗?如果我通过登录用户 firebase.auth() ,然后在通话后立即 firebase.firestore() ,不应该有身份验证用户的上下文吗?如果没有,我如何将其传递给firestore呼叫?

    谢谢

    4 回复  |  直到 7 年前
        1
  •  7
  •   d-_-b    4 年前

    经过数周的搜索,firestore和auth都独立工作,但不在一起。。。这是最简单的事情。

    我在firebase 4上。x、 这是我开始我的项目时可用的最新版本。然后,当我开始添加auth规则时,firebase上最新发布的关于如何执行db规则的文档(我刚开始时甚至没有看过,但几个月后我又回到了准备实现db规则的时候)用firebase 5列出。y、 更新了我的软件包。json、npm安装和一切神奇的工作:(

    希望这能帮助其他人。。。确保您至少在版本5上!

    使用以下内容更新模拟器: npm install -g firebase-tools

        2
  •  2
  •   David    4 年前

    我也有同样的问题,但最终对我有效的是更新 firebase cli 通过运行 npm install -g firebase-tools 这反过来又更新了 cloud firestore emulator

    注意: 根据您使用的安装方法,可能有不同的方式更新firebase cli。如果您使用 npm 安装firebase cli。有关更新firebase cli的其他方法,请参阅 firebase docs

        3
  •  2
  •   BorisD    4 年前

    2021 06月

    firebase cli 9.12.1仍然存在问题。

    必须将firebase JS降级到8.3.1才能正常工作,但在该版本中,它会尝试在线验证用户,而不是使用模拟器

    看见 this post

        4
  •  0
  •   Harish    7 年前

    您应该使用 firebase.firestore().collection('users').doc(`${currentUser.uid}`).get() 而不是 firebase.firestore().collection('users').doc(`${email}`).get()

    这是因为你的安全规则

    match /users/{userId} {
      allow create, read, update: if request.auth.uid != null;
    }
    

    在确定是否向用户而不是用户的电子邮件提供数据访问权限时,请检查用户的uid。

    希望这有所帮助。