具有身份验证的Cloud Firestore

问题描述 投票:0回答:2

我一直在寻找一种方法来使用身份验证与Firebase的新Cloud Firestore。我知道这是可能的,但在文档和其他地方都没有好的指南。

有人可以解释一下如何在从Fire Firestore中获取数据时提供身份验证UID吗?

这是我的安全规则:

get()

这是我目前的代码:

service cloud.firestore {
  match /users/{userID} {
    allow read, write: if request.auth.uid == userID;
  }
}

我的数据库结构只是root用户的“用户”集合,然后是每个用户的文档(以UID命名)。我想用用户的UID获取文档。

当然,由于安全规则,这会产生错误“缺少权限或权限不足”,这是预期的。

这个问题可能看起来很简单,但是如果有人能找到一些好的文档,那就太好了!

javascript firebase authentication firebase-authentication google-cloud-firestore
2个回答
5
投票

你的规则应该在var db = firebase.firestore(); var docRef = db.collection("users").doc(uid); //lets say "uid" var is already defined docRef.get().then(function(doc) { if (doc.exists) { console.log("Document data:", doc.data()); } else { console.log("No such document!"); } }).catch(function(error) { console.log("Error getting document:", error); });

match /databases/{database}/documents

1
投票

我只是对UID的工作方式感到困惑。我的印象是你必须为数据库操作提供UID和auth令牌。

您实际上并没有将UID传递给数据库操作。 service cloud.firestore { match /databases/{database}/documents { match /users/{userID} { allow read, write: if request.auth.uid == userID; } } } 对象存储身份验证状态。您可以简单地登录(firebase),并在操作完成后(使用check the Firebase docs to read how to do this承诺声明),您可以简单地进行操作。

此外,要让用户每次访问您的应用时都不登录,您可以.then

© www.soinside.com 2019 - 2024. All rights reserved.