Home > database >  How to retrieve all field of the current auth user in firestore? - Kotlin
How to retrieve all field of the current auth user in firestore? - Kotlin

Time:11-21

How do I retrieve all the fields of the current user logged?

enter image description here

I've watched many tutorials and questions, and some of them talk about the whole collection, others about similar topics, but found no info about this.

Thank you

UPDATE

Current Code:

  fun getUserName_FireBase(){
        if(userID==null){

            println("The userID is null")
            userID= getUserID()

            println("The userId has been assigned and now is: "   userID.toString())

        }

        println("1")

            val db = FirebaseFirestore.getInstance()
        println("1a")
        val usersRef = db.collection("users")
        println("1b")

        usersRef.document(userID.toString()).get().addOnCompleteListener { task ->
               println("2")
               if (task.isSuccessful) {
                   println("3")
                   val document = task.result
                   if(document!!.exists()){
                       println("4")
                       userName = document!!.getString("user").toString()
                        println("user is "   userName.toString())
                   }else {
                       println("5")
                       Log.d("Error", "This document does not exist")

                   }

               }else {
                   println("6")
                   task.exception?.message?.let {
                       Log.d(TAG, it)
                   }

               }
               println("7")

           }
        println("8")
    }

Console error

The error is given because later I need to acces to userName var that is supposed to be filled in that function

enter image description here

CodePudding user response:

To be able to get user data, you have to create a reference that points to that document, perform a get() call and attach a listener, as seen in the following lines of code:

val db = FirebaseFirestore.getInstance()
val usersRef = db.collection("users")
usersRef.document("gA4z1AhkQpQ6J47sIMmCGIZRKDK2").get().addOnCompleteListener { task ->
    if (task.isSuccessful) {
        val document = task.result
        if (document.exists()) {
            val email = document.getString("email")
            val pass = document.getString("pass")
            val user = document.getString("user")
            Log.d(TAG,"$email/$pass/$user")
        } else {
            Log.d(TAG, "The document doesn't exist.")
        }
    } else {
        task.exception?.message?.let {
            Log.d(TAG, it)   
        }
    }
}

The result in the logcat will be:

barrooroor@gmail.com/paport/do3fe4232ef2

If "gA4z1AhkQpQ6J47sIMmCGIZRKDK2" is the ID of the user that comes from the authentication process, then instead of the hard coded ID, you can simply use:

val auth = FirebaseAuth.getInstance()
val uid = auth.currentUser?.uid
usersRef.document(uid).get().addOnCompleteListener {/* ... /*}
//                            
  • Related