android - 用户如何只能在 Cloud Firestore 中看到自己的数据?
问题描述
我正在用 Kotlin 构建一个应用程序。在app中,用户可以注册登录。用户登录后,用户添加一些文字和图片,这些数据成功添加到firebase云存储,并以列表的形式显示在app中。但是,在应用程序上注册的任何人都可以访问此列表。
我想做的是用户如何只能看到自己的数据。所以我不希望用户看到彼此的数据。我只希望每个用户都能看到他们自己添加的数据。我认为我需要更改 Firebase Cloud Strore 规则,并且我认为 userId 必须等于 documentId 才能做到。但是我该怎么做呢?
我是新来的,谢谢!
Cloud Firestore 规则;
service cloud.firestore {
match /databases/{database}/documents {
// Allow only authenticated content owners access
match /Notes/{userId}/{documents=**} {
allow read, write: if request.auth != null && request.auth.uid ==userId
}
}
}
我保存了 useremail、noteTitle ...等。到我共享的数据库;
val noteMapElse = hashMapOf<String, Any>()
noteMapElse.put("userEmail", auth.currentUser!!.email.toString())
noteMapElse.put("noteTitle", titleText.text.toString())
noteMapElse.put("yourNote", noteText.text.toString())
noteMapElse.put("date", Timestamp.now())
//UUID -> Image Name
val uuid = UUID.randomUUID()
val imageName = "$uuid.jpg"
val storage = FirebaseStorage.getInstance()
val reference = storage.reference
val imagesReference = reference.child("images").child(imageName)
imagesReference.putFile(selectedPicture!!).addOnSuccessListener { taskSnapshot ->
// take the picture link to save the database
val uploadedPictureReference =
FirebaseStorage.getInstance().reference.child("images").child(imageName)
uploadedPictureReference.downloadUrl.addOnSuccessListener { uri ->
val downloadUrl = uri.toString()
println(downloadUrl)
noteMapElse.put("downloadUrl", downloadUrl)
db.collection("Notes").add(noteMapElse).addOnCompleteListener { task ->
if (task.isComplete && task.isSuccessful) {
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(
applicationContext,
exception.localizedMessage?.toString(),
Toast.LENGTH_LONG
).show()
}
}
}
}
这也是我的登录和注册活动;
fun signIn (view: View) {
auth.signInWithEmailAndPassword(mailText.text.toString(), passwordText.text.toString())
.addOnCompleteListener { task ->
if (task.isSuccessful) {
Toast.makeText(applicationContext, "Welcome : ${auth.currentUser?.email.toString()}",
Toast.LENGTH_LONG).show()
val intent = Intent(applicationContext, ListViewActivity::class.java)
startActivity(intent)
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(
applicationContext,
exception.localizedMessage?.toString(),
Toast.LENGTH_LONG
).show()
}
}
fun signUp (view: View) {
val email = mailText.text.toString()
val password = passwordText.text.toString()
auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener {
if (it.isSuccessful) {
Toast.makeText(applicationContext, "Your Account Has Been Created Successfully", Toast.LENGTH_LONG).show()
val intent = Intent(applicationContext, ListViewActivity::class.java)
startActivity(intent)
finish()
}
}.addOnFailureListener { exception ->
if (exception != null ) {
Toast.makeText(applicationContext,exception.localizedMessage.toString(),Toast.LENGTH_LONG).show()
}
}
}
}
解决方案
为了确保用户只能看到他们自己的笔记,您需要有某种方法来识别每个文档属于哪个用户。有两种常见的方法可以做到这一点:
- 将特定用户的所有笔记存储在包含他们自己的 UID 的路径中。
- 将所有者的 UID 存储在每个文档中。
从屏幕截图来看,您似乎遇到了第一种情况,因为 UID 似乎被用作文档 ID。在这种情况下,您可以通过以下方式保护它:
service cloud.firestore {
match /databases/{database}/documents {
// Allow only authenticated content owners access
match /some_collection/{userId}/{documents=**} {
allow read, write: if request.auth != null && request.auth.uid == userId
}
}
}
此示例直接来自有关保护内容所有者仅访问的文档,因此我建议您阅读该文档以获取更多详细信息。
推荐阅读
- android - 从回收站视图中删除多个(50 多个)项目会引发异常(附加 Stacktrace)
- node.js - 调用函数不断给我一个承诺而不是一个对象
- python - 收到错误“NameError:名称'Tk'未定义”
- python - 如何将参数传递给 django-rest-framework 中的序列化器?
- c# - 使用 LINQ 表达式生成具有相关数据和条件的查询
- laravel - Laravel - 从同一控制器中的另一个方法调用控制器方法(使用自定义表单请求验证)
- php - PHPMailer 不发送任何邮件
- ruby - 如何使用 Ruby 在命令提示符中打印 unicode 字符
- c# - 使用 RestSharp 读取响应
- java - 为什么我的表单没有在 Spring Boot 中传递信息