Firestore:FirebaseError:权限缺失或不足

时间:2021-05-25 12:45:16

标签: javascript typescript firebase google-cloud-firestore firebase-security

我有以下规则:

rules_version = '2';
service cloud.firestore {

    match /documents/{any} {
        allow read, write: if request.auth.uid != null
    }

    match /users/{any} {
        allow read, write: if request.auth.uid != null
    }
}

所以基本上,我希望经过身份验证的用户能够读/写 usersdocuments 集合。但是我在创建用户并登录时尝试写入:

                const registerRes = await firebase
                    .auth()
                    .createUserWithEmailAndPassword(email, password);
                await registerRes.user.updateProfile({
                    displayName: fullName
                });
                registerRes.user.sendEmailVerification();
                await firebase.firestore().collection('users').doc(registerRes?.user?.uid).set({...someStuff});

但我收到一个错误:

FirebaseError: Missing or insufficient permissions.

不确定我做错了什么。任何帮助将不胜感激 - 谢谢!

1 个答案:

答案 0 :(得分:1)

doc 中所述,您需要在 match /databases/{database}/documents 块中包含特定于路径的规则,例如:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    match /documents/{any} {
        allow read, write: if request.auth.uid != null
    }

    match /users/{any} {
        allow read, write: if request.auth.uid != null
    }


  }
}

旁注:请注意,基于 request.auth.uid != null 的规则并不能真正保护您的数据库,请参阅此 SO answer