Firebase应用程序不会将用户添加到我的实时数据库中

时间:2018-12-07 04:50:25

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

我正在使用Android应用程序,正在尝试将用户添加到Firebase的实时数据库中。这是在注册片段中完成的。这是它的代码:

public class RegisterFragment extends Fragment {
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment RegisterFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static RegisterFragment newInstance() { return  new RegisterFragment(); }
    private FirebaseAuth mAuth;
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("Users");

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_register,
                container, false);
        mAuth = FirebaseAuth.getInstance();
        final EditText user = view.findViewById(R.id.username);
        final EditText email = view.findViewById(R.id.email);
        final EditText password = view.findViewById(R.id.password);
        final EditText confirmPassword = view.findViewById(R.id.confirmpassword);
        final Button btnAction = view.findViewById(R.id.AddUser);
        final Button btnLogin = view.findViewById(R.id.btnLoginTab);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.copyright_frame_layout, LoginFragment.newInstance());
                ft.addToBackStack(null);
                ft.commit();
            }
        });

        btnAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (password.getText().toString().equals(confirmPassword.getText().toString()))
                    AddUser(user.getText().toString(), email.getText().toString(), password.getText().toString(), confirmPassword.getText().toString());
                else {
                    password.getText().clear();
                    confirmPassword.getText().clear();
                    Toast.makeText(getActivity(), "Passwords do not match, please reenter your password!", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
        return view;
    }
private void AddUser(final String username, final String email, final String password, final String confirmpassword){
// Write a message to the database


    mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (!task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Toast.makeText(getActivity(),"A user with this same email is found!",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    else{
                        Users user = new Users(email, password, username);
                        String messageId = myRef.push().getKey();

                        myRef.child(messageId).setValue(user);
                        Toast.makeText(getActivity(),"A new user has been added!",Toast.LENGTH_SHORT).show();
                    }
                }
            });
}
}

我的代码现在正在执行的操作是使用一些基本值(用户名,电子邮件和密码),而不是将其添加到名为Users的实时数据库中,而只是将其添加为身份验证用户之一。

enter image description here

这就是我在用户中拥有的

enter image description here

我想知道的是如何继续将用户添加到实时数据库中。

1 个答案:

答案 0 :(得分:0)

好的,所以我设法解决了这个问题。我没有意识到很多步骤(非编码)。首先,我需要将目标数据库设置为实时数据库。我的位置设为Cloud Firestore“ Beta”。

enter image description here

从那里我必须相应地设置我的读/写规则。现在,我可以创建新用户,并且现在可以在我的实时数据库中看到他们