I'm trying to write some data on my Realtime Database on Android Studio with Java. My goal is to create and user and then add user information in Realtime Database. I can create the User but when I'm trying to write on the Realtime Database nothing happen. I've add a onSuccess and onFailure listener to the function that set the data, but both of th listener doesen't run.
Code:
mAuth.createUserWithEmailAndPassword(user.getEmail(), user.getPassword())
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, "Account creato!",
Toast.LENGTH_SHORT).show();
FirebaseUser currentUser = mAuth.getCurrentUser();
user.setUid(currentUser.getUid());
//I add to RealTime Databse other User data
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("users").child("test").setValue("hey").addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i("Works","Yes");
Toast.makeText(RegisterActivity.this,"It orks",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Write failed
Log.e("ErrorAdding",e.getMessage());
Toast.makeText(RegisterActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
}
});
//finish();
} else {
// If sign in fails, display a message to the user.
Log.w("CREATEUSER", "createUserWithEmail:failure", task.getException());
Toast.makeText(RegisterActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
I'm sure the user is created because the toast with "Account creato" shows up and in firebase I can see the account added.
This is my Realtime database rules:
{
"rules": {
".read": "true", // 2022-2-4
".write": "true", // 2022-2-4
}
}
Thanks for your help!