首页 > 解决方案 > 如何使云 Firestore auth Uid 匹配用户 ID?

问题描述

Firestore 文档中,一种推荐的编写安全规则的方法是

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read, update, delete: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
    }
  }
}

但是,当我创建用户时,用户 ID 与 auth.uid 不同(两者都是随机字符串,但完全随机)。

创建新用户时如何使它们自动匹配?

标签: iosswiftfirebasegoogle-cloud-firestorefirebase-security

解决方案


您显示的规则表明文档的 ID 应与通过 Firebase 身份验证获得的用户的 UID 匹配。这是标准做法。如果您的代码也没有为此编写文档,那么该规则将无法按您的预期工作。因此,您获取用户的 UID并将其用作文档的 ID。


推荐阅读