GitLiveApp/firebase-kotlin-sdk

Add FirebaseAuthException.errorCode to "firebase-auth" for iOS/Android/...

Opened this issue · 2 comments

Hey guys! Thanks for the constellation of KMP compatible firebase libraries!
I've started my migration with firebase-auth and found out that FirebaseAuthException.errorCode(s) are omitted.
It would be nice to have the errorCodes along with the error messages. Thanks!

Screenshot 2024-11-18 at 15 56 15

I didn't get how to even get the exception -> using
auth.signInWithEmailAndPassword(email, password)
but how to implement those functions?

@TomerGlick You can try smth like this:

suspend fun signInWithEmail(email: String, password: String): FirebaseSignInResult {
    return try {
      val result = auth.signInWithEmailAndPassword(email, password)
      FirebaseSignInResult.Success
    } catch (e: Exception) {
      handleFailure(e)
    }
  }
private fun handleFailure(e: Exception): FirebaseSignInResult {
    return when (e) {
      is FirebaseAuthWeakPasswordException -> FirebaseSignInResult.Failure.WeakPassword
      is FirebaseAuthInvalidCredentialsException -> handleInvalidCredentialsFailure(e)
      is FirebaseAuthInvalidUserException -> handleInvalidUserFailure(e)
      is FirebaseAuthUserCollisionException -> FirebaseSignInResult.Failure.UserCollision
      is FirebaseAuthEmailException -> FirebaseSignInResult.Failure.InvalidEmail
      else -> FirebaseSignInResult.Failure.Unexpected
    }
  }
sealed interface FirebaseSignInResult {
  data object Success : FirebaseSignInResult
  sealed interface Failure : FirebaseSignInResult {
    data object InvalidPassword : Failure
    data object InvalidEmail : Failure
    data object WeakPassword : Failure
    data object UserDisabled : Failure
    data object UserNotFound : Failure
    data object UserCollision : Failure
    data object Unexpected : Failure
  }
}