user18559097
user18559097

Reputation:

Firebase adding user prublem -- JAVA

Hi this is my first time using Android Studio and databases in general, I can't connect my app to the DB. In the DB i have set .write and .read to "true" for both of them:

{
"rules": {
".read": "true", //"now < 1671750000000", // 2022-12-23
".write":"true" // "now < 1671750000000", // 2022-12-23
  }
}

But Whenever I try to register a user it doesn't work. This is my code in JAVA in case it helps:

    //DATABASE
//Checking if the user is already registered
mAuth.createUserWithEmailAndPassword(email,password)
        .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                //if the user has been registered
                if(task.isSuccessful()){
                    User user = new User(fullname, age, email);

                    //sending the user to the DATABASE
                    FirebaseDatabase.getInstance().getReference("Users")
                            .child(FirebaseAuth.getInstance().getCurrentUser().getUid()) //This will return the ID of the Registered User
                            .setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    //If the user has been registered and has been inserted into the DB
                                    if(task.isSuccessful()){
                                        Toast.makeText(registerUser.this, "El usuario se ha registrado.", Toast.LENGTH_LONG).show();
                                        pBprogressbar.setVisibility(View.GONE);
                                    }
                                    //if the user doesn't register
                                    else{
                                        Toast.makeText(registerUser.this, "Ha habido un fallo en el registro", Toast.LENGTH_LONG).show();
                                        pBprogressbar.setVisibility(View.GONE);
                                    }
                                }
                            });
                } else {
                    Toast.makeText(registerUser.this, "Ha habido un fallo en el registro", Toast.LENGTH_LONG).show();
                    pBprogressbar.setVisibility(View.GONE);
                }
            }
        });
    }
}

I have run the Debugger on it and this is what it comes out: enter image description here

But this is my User constructor:

package com.example.kmeco;

//So we store the user information in the object. And then we send it to FIREBASE public class User { public String fullName, age, email;

//Creating two constructors
//First one is an empty public constructor that doesn't accept or return anything
public User(){

}
//Second constructor
public User(String fullName, String age, String email){
    //initializing values
    this.fullName = fullName;
    this.age = age;
    this.email = email;
}

}

The problem is in the first:

if(task isSuccessful())

And I don't know why it doesn't read it,

I would appreciate any help

Upvotes: 0

Views: 44

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

If a task fails it contains an exception with information about why it failed. You should log that exception, to find out the root cause of the problem:

mAuth.createUserWithEmailAndPassword(email,password)
    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            //if the user has been registered
            if(task.isSuccessful()){
                ...
            } else {
                // 👇
                Log.e("AUTH", "createUserWithEmailAndPassword failed", task.getException());
                ...
            }
        }
    });

Upvotes: 0

Related Questions