如何将Firebase身份验证与数据库中的现有用户连接?

时间:2018-09-01 09:18:52

标签: android firebase firebase-realtime-database firebase-authentication

我有一个应用程序,用户可以在Firebase数据库中注册该应用程序并将其数据。 Firebase构建在Firebase数据库根用户中是这样的:

"test" : {
      "email" : "test@gmail.com",
      "gender" : "Male",
      "password" : "test",
      "userName" : "test",
      "user_class" : "Barbarian"
    },

我想使用电子邮件和密码添加Firebase身份验证,但我想保留数据库结构(具有用户字段)。我该怎么办?

这是我在数据库中创建用户的提交表单:

private void submitForm() {


        String user_class = String.valueOf(spinner.getSelectedItem());

        int selectedId = genderRadioGroup.getCheckedRadioButtonId();
        String gender;
        if (selectedId == R.id.female_radio_btn)
            gender = "Female";
        else
            gender = "Male";

        final User user = new User(signupInputName.getText().toString(),
                signupInputPassword.getText().toString(),
                signupInputEmail.getText().toString(),
                gender,
                user_class);

        users.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.child(user.getUserName()).exists())
                    Toast.makeText(RegisterActivity.this, "User already exists! ", Toast.LENGTH_SHORT).show();
                else {
                    users.child(user.getUserName())
                            .setValue(user);
                    Toast.makeText(RegisterActivity.this, "User registration success! ", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(
                            RegisterActivity.this,
                            MainActivity.class);
                    startActivity(intent);
                    finish();

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

0 个答案:

没有答案