0

This code should create a user in firebase, but there was not a single entry there:

 private fun enterCode() {
        val code: String = binding.registerInputCode.text.toString();
        val credential = PhoneAuthProvider.getCredential(id, code);

        AUTH.signInWithCredential(credential).addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val uid = AUTH.currentUser?.uid.toString();
                val dateMap = mutableMapOf<String, Any>();
                dateMap[CHILD_ID] = uid;
                dateMap[CHILD_PHONE] = mPhoneNumber
                dateMap[CHILD_USERNAME] = uid;

                REF_DATABASE_ROOT.child(NODE_USER).child(uid).updateChildren(dateMap)
                    .addOnCompleteListener { task2 ->
                        if (task2.isSuccessful) {
                            showToast("Wellcome to the app")
                            (activity as RegisterActivity).replaceActivity(MainActivity())
                        } else
                            showToast(task2.exception?.message.toString())
                    }

            } else
                showToast(task.exception?.message.toString())
        }
    }

//another file with the authentication and the database reference

lateinit var AUTH: FirebaseAuth;
lateinit var REF_DATABASE_ROOT: DatabaseReference

const val NODE_USER = "users"
const val CHILD_ID = "id"
const val CHILD_PHONE = "phone"
const val CHILD_USERNAME = "username"

fun initFirebase(){
    AUTH = FirebaseAuth.getInstance();
    REF_DATABASE_ROOT = FirebaseDatabase.getInstance().reference
}
// my dependencies
    // firebase
    def firebaseVer = "22.0.0"
    implementation "com.google.firebase:firebase-auth-ktx:$firebaseVer"
    implementation "com.google.firebase:firebase-auth:$firebaseVer"

    implementation 'com.google.firebase:firebase-firestore:24.6.1'
    implementation 'com.google.firebase:firebase-database-ktx:20.2.2'
    implementation 'com.google.firebase:firebase-database:20.2.2'

My Realtime Database this is the rules

'task' completed successfully, 'dateMap' was not null and filled with correct data. "REF_DATABASE_ROOT" had been correctly initialized. But I didn't get "task2" at all, nothing happened. What am I doing wrong and what should I do to fix the problem?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Serge
  • 1
  • 1
    What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Jul 19 '23 at 11:43
  • If you're looking to implement Firebase sign-in with email and password, then I think that this [resource](https://medium.com/firebase-developers/how-to-authenticate-to-firebase-using-email-and-password-in-jetpack-compose-bd70ca56ea91) will help. Here is the corresponding [repo](https://github.com/alexmamo/FirebaseSignInWithEmailAndPassword). – Alex Mamo Jul 19 '23 at 11:43
  • It sounds like your application might not be configured correctly, and can't find the database. If you configured the app through `google-services.json`, download an updated version of that and add it to your app. Alternatively, you can specify the database URL in your code when you call `FirebaseDatabase.getInstance()`. For more on this, see https://stackoverflow.com/questions/68173632/google-firebase-real-time-database-not-working-as-everything-is-set-correctly/68179677#68179677 – Frank van Puffelen Jul 19 '23 at 13:17
  • Thanks a lot guys, the problem was with google-services.json. I downloaded the google-services.json again and replaced it (the new file contained a firebase url which was not in the old version of the google-services.json file) – Serge Jul 20 '23 at 08:27

0 Answers0