在Firestore中,如何获取Firebase身份验证的uid作为文档?

时间:2019-11-14 06:09:30

标签: android firebase google-cloud-firestore

如何获取firebase身份验证的firebase当前uid作为文档?我是初学者。

这是我要获取ID的Java文件

public class Question extends AppCompatActivity {

    private FirebaseFirestore db = getInstance();
    FirebaseUser user;
    private EditText e1,e2,e3;
    FirebaseAuth mAuth;
    private Button b1;

    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);

        e1 = findViewById(R.id.editText3);
        e2 = findViewById(R.id.editText4);
        e3 = findViewById(R.id.editText5);
        b1 = findViewById(R.id.button4);

        progressDialog = new ProgressDialog(Question.this);
        progressDialog.setTitle("OSIX");
        progressDialog.setMessage("Saving Your Opinion.....");

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String question1 = e1.getText().toString();
                final String question2 = e2.getText().toString();
                final String question3 = e3.getText().toString();

                if (question1.isEmpty()){
                    e1.setError("Field is Empty");
                    e1.requestFocus();
                } else if (question2.isEmpty()){
                    e2.setError("Field is Empty");
                    e2.requestFocus();
                }else if (question3.isEmpty()){
                    e3.setError("Field is Empty");
                    e3.requestFocus();
                }else {
                    progressDialog.show();
                    user = mAuth.getCurrentUser();
                    assessment answer = new assessment(question1,question2,question3);
                    db.collection("Assessment").document().set(answer);
                    startActivity(new Intent(Question.this, Navbar.class));
                    progressDialog.show();
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

使用这种方法来获取当前用户的uid

private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;
private DatabaseReference databaseReferenceRoot;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question);

    firebaseAuth = FirebaseAuth.getInstance();
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReferenceRoot = firebaseDatabase.getReference();
    String uid=firebaseAuth.getCurrentUser().getUid()
    //Here you get the uid

}