我制作了一个按钮,该按钮应允许用户根据其用户ID删除所有数据,该按钮的目的与“删除帐户”按钮相同,但不起作用。我尝试使用以下代码删除集合中的所有文档:
final String userid = FirebaseAuth.getInstance.getCurrentUser().getUid();
db.collection("main").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (QueryDocumentSnapshot queryDocumentSnapshot : task.getResult()){
db.collection("main").document(userid).delete();
}
}
})
有一个runtimeExecution:没有权限错误
com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.
我将安全规则设置如下,以便其他用户无法访问其他用户的数据。
service cloud.firestore {
match /databases/{database}/documents {
match /main/{userId}/{document=**} {
allow read, write: if request.auth.uid == userId;
}
}
}
不知道该怎么办,我认为安全规则可以防止安全问题,但是我认为这会导致此权限错误?
先感谢您。
更新:
经过研究并在该站点上阅读了有关此问题的许多其他答案之后,我使用了这种方法,我认为对于像我这样的假人来说这是如此简单,但从未在其他答案中提及我不确定原因:
为了将用户从身份验证中完全删除,我使用了名为“删除用户数据”的Firestore扩展,这是一个很大的帮助,它非常神奇,它使我可以通过使用以下代码删除用户:
FirebaseAuth.getInstance().getCurrentUser().delete().addOnSuccessListener(new OnSuccessListener<Void>() {...}
它不仅删除用户身份验证,而且仅删除该代码,Firestore中特定用户的数据以及存储中的图像也消失了。太棒了!重要的一点是,您必须从身份验证连接用户UID,并将UID连接到Firestore和存储。
要了解有关如何使用“删除用户数据” Firestore扩展的更多信息(它仍处于beta模式),请尝试查看JORGE VERGARA的这篇很棒的博客教程:安装Delete User Data扩展
如何使用扩展程序:它非常简单,我很高兴能帮助我轻松地删除用户而不必大惊小怪
我的Firestore的结构:
main(collection)--->userID---->journal(collection)----->journalIDdsvsfbsf
----->journalIDdfvdbgnd
--->userID2--->journal(collection)----->journalIDdsvsfbsf
----->journalIDdfvdbgnd
了解如何将Firestore和存储与同一用户UID连接,以便Firestore扩展可以通过删除身份验证轻松删除用户。
If you still want to use the old method: I noticed that you cannot delete a firestore document that contains one or multiple sub-collections with just delete method. For example, if you want to delete a document that contains 3 sub-collections, in order to delete that document(a user in my case), you have to get all the documents in one of the sub-collection, delete them all, then proceed to do the same to all the other sub-collections in that document. Once all the sub-collections in the document has been deleted, then the document that contains them will be gone itself. That's how Firestore works it seems.
To put it in code: to delete a document that contains a sub-collection "journal" Without using the extension, I have to do it like this:
FirebaseFirestore.getInstance().collection("main").document(userid).collection("journal").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (final QueryDocumentSnapshot document : task.getResult()) {
if (document.exists()){
db.collection("main").document(userid).collection("journal").document(document.getId()).delete().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(SettingsActivity.this, "Journal: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(SettingsActivity.this, "No data in JOURNAL", Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(SettingsActivity.this, "Journal task: " + task.getException(), Toast.LENGTH_LONG).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(SettingsActivity.this, "Error getting all the documents: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
如果该特定文档中有多个子集合,则要删除该子集合,必须针对不同的子集合多次重复该代码,这会使代码很长。
我希望这对试图从Firestore中删除用户的人有所帮助。:)已经使用扩展名。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句