0
votes

I am a beginner in Firebase android development using Kotlin, my problem is I want to detect specific FirebaseAuthUserException using Kotlin. How do I achieve the desired result.

I have seen examples using JAVA but I could not figure it out how to implement it using Kotlin. Any Help is highly appreciated. Below is what I have written in my code:


private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) 
{
        mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this) 
              { task ->
                    if (task.isSuccessful) 
                      {
                          progressBar.visibility=View.INVISIBLE
                          gotomainactivity(
                          mAuth.currentUser!!.uid.toString()
                          ,phoneNumber_to_verify)
                       }
                else
                {
                    //Handle task if not sign inis not sucessful due to phone 
                     number already exist
                    val message=task.exception.toString()
                    Toast.makeText(this@RegisterActivity,"Error: 
                    $message",Toast.LENGTH_SHORT).show()
                    progressBar.visibility=View.INVISIBLE

                }
            }
}

I expect to display to users using a toast if phone number already exist.

1

1 Answers

0
votes

When using the following line of code:

val message=task.exception.toString()

You are getting the String representation of the exception object, more precisely the address of the object from the memory. What you should do instead is to get the message out of the exception. So please change the above line of code to:

val message=task.exception!!.message