0

I want to add a new user in Firebase Firestore via kotlin. But i can't get positive solution to add user details for firestore. Actually it gives information about if there has a existing username but i did not add new document. When i add there existing username it show toast message and log. But the second chance which i make to do, does not works correctly, neither happening antyhing.

private fun addUserToFireStore(


        ) {
            auth.createUserWithEmailAndPassword(email, password).addOnSuccessListener {

                updateUser()

            }.addOnFailureListener { e ->

            }

        }


        private fun updateUser() {
            val rootRef = FirebaseFirestore.getInstance()
            val allUsersRef = rootRef.collection("ProfPic")
            val cu = auth.currentUser
            val userNameQuery = allUsersRef.whereEqualTo("Username", username)
            userNameQuery.get().addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    for (document in task.result) {

                        if (document.exists()) {
                            Log.d("TAG", "username already exists")
                            Toast.makeText(
                                this@Logmenu2,
                                "Exist",
                                Toast.LENGTH_LONG
                            ).show()
                            //Do what you need to do with the userName
                        } else {
                            Toast.makeText(
                                this@Logmenu2,
                                "Created",
                                Toast.LENGTH_LONG
                            ).show()

                            val profID = auth.currentUser?.uid
                            val user = hashMapOf(
                                "Username" to username,
                                "Email" to email,
                                "Fullname" to fullname,
                                "Rank" to rank,
                                "Rating" to raiting,
                                "downloadUs" to downloadUrl,
                                "userID" to profID
                            )
                            fstore.collection("ProfPic").document(profID.toString()).set(user)
                                .addOnSuccessListener { documentReference ->


                                }
                            Toast.makeText(
                                this@Logmenu2,
                                "Created",
                                Toast.LENGTH_LONG
                            ).show()

                        }
                    }
                } else {
                    Log.d("TAG", "Error getting documents: ", task.exception)
                }
            }


        }

    }

UPDATE:

These code below worked for me

var userExist = false
            val mQuery: Query =
                fstore.collection("ProfPic")
                    .whereEqualTo("Username", username)
            mQuery.get().addOnCompleteListener { task ->
                Log.d(ContentValues.TAG, "checkingIfusernameExist: checking if username exists")
                if (task.isSuccessful) {
                    for (document in task.result) {
                        val userNames = document.getString("Username")
                        if (userNames == username) {
                            Log.d(
                                ContentValues.TAG,
                                "checkingIfusernameExist: FOUND A MATCH -username already exists"
                            )
                            Toast.makeText(
                                this,
                                "username already exists",
                                Toast.LENGTH_SHORT
                            )
                                .show()
                            userExist = true
                            break

                        }
                    }
                    if (userExist == false) {

0 Answers0