简体   繁体   中英

Firebase Authentication User UID as child node in Realtime Database

I'm trying to create a folder in my Realtime Database called "users" with the unique identifier being the UID Firebase creates.

This code worked a few months ago. But it doesn't anymore. Frankly, I've made so many changes, I don't know if it was something I deleted... But I can't find what's wrong. The user gets created, but nothing gets written inside "users" in my Realtime Database.

                auth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            auth.signInWithEmailAndPassword(inputEmail.getText().toString(),inputPassword.getText().toString());
                            Toast.makeText(SignUpActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
                            progressBar.setVisibility(View.GONE);
                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                            if (!task.isSuccessful()) {
                                Toast.makeText(SignUpActivity.this, "Authentication failed." + task.getException(),
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                auth.signInWithEmailAndPassword(inputEmail.getText().toString(),inputPassword.getText().toString());
                                FirebaseUser firebaseUser = auth.getCurrentUser();
                                String userid = firebaseUser.getUid();
                                final User newuser = new User(signupInputUsername.getText().toString().trim(), inputEmail.getText().toString().trim(), inputPassword.getText().toString().trim(),
                                        gender.getText().toString().trim(), age.getText().toString().trim());
                                mDatabase.child("users").child(userid).setValue(newuser);
                                startActivity(new Intent(SignUpActivity.this, UserActivity.class));
                                finish();
                            }
                        }
                    });

 @IgnoreExtraProperties
public static class User {

    public String username, inputEmail, inputPassword, gender, age;

    public User() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public User(String username, String inputEmail, String inputPassword, String gender, String age) {
        this.username = username;
        this.inputEmail = inputEmail;
        this.inputPassword = inputPassword;
        this.gender = gender;
        this.age = age;
    }

}

look at the following code its from your code

mDatabase.child("users").child(userid).setValue(newuser);
startActivity(new Intent(SignUpActivity.this, UserActivity.class));
finish();

the method .setValue actually gives you a task its not a void function so you should attach a listener to it and look if the the task was completed.

if it worked before it was still a problem in your code.

moreover look for the db rules if they are ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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