在 firebase 安全规则中使用提供者 uid

Using the provider uid in firebase security rules

我有一个仅使用 Google 身份验证的 Firebase 应用程序。我想让从未通过我的 Firebase 项目进行身份验证的用户根据身份验证提供程序的 uid 访问部分数据库。我想使用目录 API 检索 Google uid,然后将它们存储在白名单节点中。

whitelist: {
  12345678: true,
  23456789`: true
},
data: {}

那么我想在安全规则中做这样的事情:

"rules": {
  "data": {
    ".read": "root.child('whitelist/' + auth.providerUid).exists()
  }
}

或者像这样:

"rules": {
  "data": {
    ".read": "root.child('whitelist/' + auth.providerData[0].uid).exists()
  }
}

但是是否可以访问安全规则中的提供者uid?如果是这样,它是如何工作的?

根据reference documentation语法似乎略有不同:

firebase.identities

Dictionary of all the identities that are associated with this user's account. The keys of the dictionary can be any of the following: email, phone, google.com, facebook.com, github.com, twitter.com. The values of the dictionary are arrays of unique identifiers for each identity provider associated with the account. For example, auth.token.firebase.identities["google.com"][0] contains the first Google user ID associated with the account

看来你需要auth.token.firebase.identities["google.com"][0]。我必须承认我从来没有使用过这个,因为我的安全规则只依赖于用户的主 ID:auth.uid.