Home > front end >  sending data to a firestore database | Activity | Kotlin
sending data to a firestore database | Activity | Kotlin

Time:09-25

I would like to make the user data be sent to the Firestore database upon registration. I have already made the registration mechanism, but I don't know how to send this data to the database. I couldn't find the exact answer or code anywhere on how to do it, unfortunately :(

my RegisterActivity:

    class RegisterActivity : AppCompatActivity() {
    
        private val mailAuth = FirebaseAuth.getInstance()
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_register)
    
            findViewById<TextView>(R.id.tv_login).setOnClickListener {
    
                startActivity(Intent(this@RegisterActivity, LoginActivity::class.java))
                finish()
            }
    
                    findViewById<TextView>(R.id.tv_login).setOnClickListener {

            startActivity(Intent(this@RegisterActivity, LoginActivity::class.java))
            finish()
        }

        findViewById<Button>(R.id.btn_register).setOnClickListener {
            when {
                TextUtils.isEmpty(findViewById<EditText>(R.id.et_register_email).text.toString().trim { it <= ' ' }) -> {
                    Toast.makeText(
                        this@RegisterActivity,
                        "Please enter email.",
                        Toast.LENGTH_SHORT
                    ).show()
                }

                TextUtils.isEmpty(findViewById<EditText>(R.id.et_register_password).text.toString().trim { it <= ' ' }) -> {
                    Toast.makeText(
                        this@RegisterActivity,
                        "Please enter password.",
                        Toast.LENGTH_SHORT
                    ).show()
                }
                else -> {

                    val email: String = findViewById<EditText>(R.id.et_register_email).text.toString().trim { it <= ' ' }
                    val password: String = findViewById<EditText>(R.id.et_register_password).text.toString().trim { it <= ' ' }

                    FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
                        .addOnCompleteListener(


                            OnCompleteListener<AuthResult> { task ->

                                if (task.isSuccessful) {


                                    val rootRef = FirebaseFirestore.getInstance()
                                    val usersRef = rootRef.collection("users")
                                    val auth = FirebaseAuth.getInstance()
                                    auth.currentUser?.apply {
                                        usersRef.document(uid).set(mapOf(
                                            "uid" to uid,
                                            "email" to email
                                        )).addOnCompleteListener{ task ->
                                            if (task.isSuccessful) {
                                                Log.d("TAG", "User successfully added.")
                                            } else {
                                                Log.d("TAG", task.exception!!.message!!)
                                            }
                                        }
                                    }


                                    val firebaseUser: FirebaseUser = task.result!!.user!!


                                    Log.d(
                                        "RegisterActivity", "register-in"
                                    )
                                    val intent = Intent(this@RegisterActivity, NavigationActivity::class.java)
                                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                                    intent.putExtra("user_id", firebaseUser.uid)
                                    intent.putExtra("email_id", email)
                                    startActivity(intent)
                                    finish()
                                } else {
                                    Toast.makeText(
                                        this@RegisterActivity,
                                        task.exception!!.message.toString(),
                                        Toast.LENGTH_SHORT
                                    ).show()
                                }
                            }
                        )
                }
            }
        }
    }

    override fun onStart() {
        super.onStart()
        isCurrentUser()
    }
    private fun isCurrentUser() {
        mailAuth.currentUser?.let {
            val intent = Intent(applicationContext, NavigationActivity::class.java).apply {
                flags = (Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
            }
            startActivity(intent)
        }

    }
}

<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3<3 <3 <3 <3 <3 <3 <3 <3

CodePudding user response:

In order to be able to write user data to Firestore, you have to be sure that the user is authenticated. So please add the following lines of code:

val rootRef = FirebaseFirestore.getInstance()
val usersRef = rootRef.collection("users")
val auth = FirebaseAuth.getInstance()
auth.currentUser?.apply {
    usersRef.document(uid).set(mapOf(
        "uid" to uid,
        "email" to email
    )).addOnCompleteListener{ task ->
        if (task.isSuccessful) {
            Log.d("TAG", "User successfully added.")
        } else {
            Log.d("TAG", task.exception!!.message!!)
        }
    }
}

Right inside:

if (task.isSuccessful) {
    ...
}

The result in the logcat will be:

Firestore-root
  |
  --- users (collection)
       |
       --- $uid (document)
            |
            --- uid: "The uid that comes from the authentication process"
            |
            --- email: "The email address used to authenticate"
  • Related