How to save data to Firestore database in android using anonymous authentication?

I want to know, how can I save data to my firestore database anonymously, here is how I do it un authenticated.

FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference ref = db.collection("mycollection");
Map<String, Object> data = new HashMap<>();
data.put("name", "Hii");
ref.add(data).addOnSuccessListener(documentReference -> startActivity(new Intent(submit.this, yay.class))).addOnFailureListener(e -> Toast.makeText(submit.this, "Failed to send! Try again!",Toast.LENGTH_SHORT).show());

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

If you want to use anonymous authentication, you don’t change the way you write Cloud Firestore code at all. All you have to do is sign in the user using Firebase Authentication anonymously, and the Firestore code will automatically use the anonymous signin credentials. Your security rules should be prepared for this situation.