0

I have made a simple Login App with Firebase Auth. I can register new user but the user info doesn't store in my realtime database. After I registered a new user I can log that user in but the firstname and the lastname doesn't get stored in the database. What could be wrong?

Here is the code:

class RegisterActivity : AppCompatActivity() {

    lateinit var auth: FirebaseAuth
    var databaseReference :  DatabaseReference? = null
    var database: FirebaseDatabase? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)

        auth = FirebaseAuth.getInstance()
        database = FirebaseDatabase.getInstance()
        databaseReference = database?.reference!!.child("profile")



        register()
    }

    private fun register() {


        registerButton.setOnClickListener {

            if(TextUtils.isEmpty(firstnameInput.text.toString())) {
                firstnameInput.setError("Please enter first name ")
                return@setOnClickListener
            } else if(TextUtils.isEmpty(lastnameInput.text.toString())) {
                firstnameInput.setError("Please enter last name ")
                return@setOnClickListener
            }else if(TextUtils.isEmpty(usernameInput.text.toString())) {
                firstnameInput.setError("Please enter user name ")
                return@setOnClickListener
            }else if(TextUtils.isEmpty(passwordInput.text.toString())) {
                firstnameInput.setError("Please enter password ")
                return@setOnClickListener
            }


            auth.createUserWithEmailAndPassword(usernameInput.text.toString(), passwordInput.text.toString())
                .addOnCompleteListener {
                    if(it.isSuccessful) {
                        val currentUser = auth.currentUser
                        val currentUSerDb = databaseReference?.child((currentUser?.uid!!))
                        currentUSerDb?.child("firstname")?.setValue(firstnameInput.text.toString())
                        currentUSerDb?.child("lastname")?.setValue(lastnameInput.text.toString())

                        Toast.makeText(this@RegisterActivity, "Registration Success. ", Toast.LENGTH_LONG).show()
                        finish()

                    } else {
                        Toast.makeText(this@RegisterActivity, "Registration failed, please try again! ", Toast.LENGTH_LONG).show()
                    }
                }
        }
    }
Alexander
  • 55
  • 8
  • 1
    Many things could be wrong. But if this database is hosted in Europe or Asia, the first step would be to add its URL to the call to `FirebaseDatabase.getInstance()`. See https://stackoverflow.com/questions/67795058/getinstance-doesnt-work-with-other-location-than-us-central1-in-realtime-data/67802032#67802032 – Frank van Puffelen Jun 15 '21 at 20:39
  • That was my answer, thank you very much! – Alexander Jun 18 '21 at 20:13

0 Answers0