如何仅使用Firestore为当前登录的用户存储数据 [英] How to only store data for current logged in user using Firestore

查看:31
本文介绍了如何仅使用Firestore为当前登录的用户存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是允许用户将数据存储到Firestore中的代码.问题是,登录到该应用程序的所有用户都可以查看上传的数据. 我如何才能仅允许当前登录的用户仅存储其帐户的数据?

Below is the code which allows the user to store data into Firestore. The problem is, the uploaded data is viewable for all the users logged into the app. How would I be able to only allow the current user logged in to store the data for their account only?

在Firebase数据库中,我使用以下代码(有效,但不适用于Firestore)

With Firebase Database I use the following code (which works, but not for Firestore)

FirebaseUser用户= FirebaseAuth.getInstance().getCurrentUser(); 字符串userId = user.getUid(); mDatabaseRef = FirebaseDatabase.getInstance().getReference(FB_DATABASE_PATH).child(userId);

FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser(); String userId=user.getUid(); mDatabaseRef = FirebaseDatabase.getInstance().getReference(FB_DATABASE_PATH).child(userId);

上面的代码行仅允许已登录用户存储和检索数据.因此,数据仅对他们而言是唯一的.

The above lines of code allows the data to be stored and retrieved only for the logged in user. So the data is unique to them only.

如何为Firestore复制它?

How can I replicate that for Firestore?

 fetch=findViewById(R.id.fetchDocument);

    firestore = FirebaseFirestore.getInstance();

    upload =findViewById(R.id.uploadText);

    upload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            POJO pojo = new POJO();
            pojo.setValue("Hello World");

            //below code creates collection, creates a inner doc. and uploads the value (pojo)

            firestore.collection("RootCollection").document("MyDoc")
                    .set(pojo).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {


                    //gets called if upload is successful
                    Toast.makeText(ichild.this,"Upload is successful", Toast.LENGTH_SHORT).show();

                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(ichild.this,"Upload is not successful" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });

        }
    });

    fetch.setOnClickListener(new View.OnClickListener() {

推荐答案

如何仅使用Firestore为当前登录的用户存储数据

How to only store data for current logged in user using Firestore

如果将uid传递给document()方法,则可以实现相同的目的.因此,以下代码行在Firebase实时数据库中使用:

You can achieve the same thing if you pass the uid to the document() method. So the following lines of code used in Firebase real-time database:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userId = user.getUid();
mDatabaseRef = FirebaseDatabase.getInstance().getReference(FB_DATABASE_PATH).child(userId);

在Firestore中等同于:

Is equivalent in Firestore with:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference uidRef = rootRef.collection("users").document(uid);
POJO pojo = new POJO();
uidRef.set(pojo);

这篇关于如何仅使用Firestore为当前登录的用户存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆