Luke
Luke

Reputation: 1

Firebase Authenticated but not storing in real time database

I have a login page which when registering a new user authenticates the data if it is validated and then is meant to store them. However, after the data is authenticated it doesn't add to the real time database.

Register java code:

        progressBar.setVisibility(View.VISIBLE);

        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if(task.isSuccessful()){
                    User user = new User(fullname, email);

                    FirebaseDatabase.getInstance().getReference("Users")
                            .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                            .setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if(task.isSuccessful()){
                                        progressBar.setVisibility(View.GONE);
                                        Toast.makeText(RegisterActivity.this, "User has registered successfully", Toast.LENGTH_LONG).show();
                                    }
                                    else{
                                        Toast.makeText(RegisterActivity.this, "Failed to register", Toast.LENGTH_LONG).show();
                                    }
                                    progressBar.setVisibility(View.GONE);
                                }
                            });
                }
                else{
                    Toast.makeText(RegisterActivity.this, "Failed to register22", Toast.LENGTH_LONG).show();
                    progressBar.setVisibility(View.GONE);
                }
            }
        });
    }
}

Dependencies:

implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'com.google.firebase:firebase-auth:21.0.4'
    implementation 'com.google.firebase:firebase-database:20.0.5'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

I have set the rules of the database as below:

{
  "rules": {
    ".read": true,  // 2022-6-14
    ".write": true,  // 2022-6-14
  }
}

Finally my database also only shows this:

https://hobby-tracker-a914f-default-rtdb.europe-west1.firebasedatabase.app/
:
null

Does anyone have any clue why this is happening?

Upvotes: 0

Views: 731

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

If neither of the toasts you have in the onComplete method are showing, then it may be that you downloaded the google-services.json file before you created your database. If that's the case, the file doesn't contain the database URL, and thus the client can't find your database on the server.

In that case, you can solve it by:

  • either download an updated google-services.json file, replacing the one in your project with it, and rebuilding the app.
  • or you can specify the URL for your database in the code, like this: FirebaseDatabase.getInstance("https://hobby-tracker-a914f-default-rtdb.europe-west1.firebasedatabase.app/").getReference("Users")...

Also see: Google Firebase Real-time Database Not Working as everything is set correctly

Upvotes: 1

Related Questions