简体   繁体   English

如何在 Firebase Firestore 中添加自动 uid 字段

[英]How to add auto uid field in Firebase Firestore

I want to add an auto uid field in the firebase firestore when a user register their account.当用户注册他们的帐户时,我想在 firebase 防火墙中添加一个自动 uid 字段。 How to implement that in my codes to add uid which is generated by the firebase?如何在我的代码中实现它以添加由 firebase 生成的 uid?

Here is my register.java codes:这是我的寄存器。java 代码:

public class Register extends AppCompatActivity {

//Variables
TextInputLayout username, email, PhoneNo, password;
RadioGroup radioGroup;
RadioButton selectedElderly, selectedGuardian;
Button regBtn, regToLoginBtn;

FirebaseAuth fAuth;
FirebaseFirestore fStore;

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

    fAuth = FirebaseAuth.getInstance();
    fStore = FirebaseFirestore.getInstance();

    //Hooks to all xml elements in activity_register.xml
    username = findViewById(R.id.reg_username);
    email = findViewById(R.id.reg_email);
    PhoneNo = findViewById(R.id.reg_phoneNo);
    password = findViewById(R.id.reg_password);
    regBtn = findViewById(R.id.reg_btn);
    regToLoginBtn = findViewById(R.id.reg_login_btn);
    radioGroup = findViewById(R.id.radio_type);
    selectedGuardian = findViewById(R.id.radioGuardian);
    selectedElderly = findViewById(R.id.radioElderly);

    regToLoginBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Register.this, Login.class);
            startActivity(intent);
        }
    });

    regBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (validateUsername() && validateEmail() && validatePhoneNo() && validateUserType() && validatePassword() == true) {
                Intent intent = new Intent(Register.this, Login.class);
                startActivity(intent);
            } else {
                validateUsername();
                validateEmail();
                validatePhoneNo();
                validateUserType();
                validatePassword();
            }
            fAuth.createUserWithEmailAndPassword(email.getEditText().getText().toString(), password.getEditText().getText().toString()).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult authResult) {
                    FirebaseUser user = fAuth.getCurrentUser();
                    Toast.makeText(Register.this, "Account Created", Toast.LENGTH_SHORT).show();
                    DocumentReference df = fStore.collection("Users").document(user.getUid());
                    Map<String, Object> userInfo = new HashMap<>();
                    userInfo.put("Username", username.getEditText().getText().toString());
                    userInfo.put("Email", email.getEditText().getText().toString());
                    userInfo.put("phoneNo", PhoneNo.getEditText().getText().toString());
                    userInfo.put("Password",password.getEditText().getText().toString());
                    //specify the user is elderly
                    if (selectedElderly.isChecked()) {
                        userInfo.put("isElderly", "1");
                    }
                    if (selectedGuardian.isChecked()) {
                        userInfo.put("isGuardian", "1");
                    }

                    df.set(userInfo);
                    if (selectedElderly.isChecked()) {
                        startActivity(new Intent(getApplicationContext(), Login.class));
                        finish();
                    }
                    if (selectedGuardian.isChecked()) {
                        startActivity(new Intent(getApplicationContext(), Login.class));
                        finish();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(Register.this, "Failed to Create Account", Toast.LENGTH_SHORT).show();
                }
            });

                }
    });
}
}

My database structure is:我的数据库结构是:

database structure数据库结构

As in the image, how to add one more field that contains auto-generated uid by the firebase?如图所示,如何再添加一个包含 firebase 自动生成的 uid 的字段?

If you want to add a generated id to the document, then you can do:如果要将生成的 id 添加到文档中,则可以执行以下操作:

String id = fStore.collection("Users").document().getId();

This will generate a random id, then you can do:这将生成一个随机 id,然后您可以执行以下操作:

DocumentReference df = fStore.collection("Users").document(user.getUid());
Map<String, Object> userInfo = new HashMap<>();

userInfo.put("Username", username.getEditText().getText().toString());
userInfo.put("Email", email.getEditText().getText().toString());
userInfo.put("phoneNo", PhoneNo.getEditText().getText().toString());
userInfo.put("Password",password.getEditText().getText().toString());
userInfo.put("id",id);

df.set(userInfo);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM