How to catch a Firebase Auth specific exceptions

前端 未结 16 2338
慢半拍i
慢半拍i 2020-11-28 04:37

Using Firebase, how do I catch a specific exception and tell the user gracefully about it? E.g :

FirebaseAuthInvalidCredentialsException: The email ad

16条回答
  •  日久生厌
    2020-11-28 05:24

    SOLUTION WITH KOTLIN

     fun signInWithEmail(email: String, passKey: String) {
        FirebaseAuth.getInstance().signInWithEmailAndPassword(email, passKey).addOnSuccessListener {
            it.user?.let {
                authResultOperation.postValue(AuthResultOperation.OnSuccessSignIn)
            }
        }.addOnFailureListener {
            val errorCode = (it as FirebaseAuthException).errorCode
            val errorMessage = authErrors[errorCode] ?: R.string.error_login_default_error
            Toast.makeText(context, context.getString(errorMessage),Toast.LENGTH_LONG).show()
        }
    }
    

    Explanation: Basically It's just a map that match firebase error codes with a custom string resource.

    val authErrors = mapOf("ERROR_INVALID_CUSTOM_TOKEN" to R.string.error_login_custom_token,
            "ERROR_CUSTOM_TOKEN_MISMATCH" to R.string.error_login_custom_token_mismatch,
            "ERROR_INVALID_CREDENTIAL" to R.string.error_login_credential_malformed_or_expired,
            "ERROR_INVALID_EMAIL" to R.string.error_login_invalid_email,
            "ERROR_WRONG_PASSWORD" to R.string.error_login_wrong_password,
            "ERROR_USER_MISMATCH" to R.string.error_login_user_mismatch,
            "ERROR_REQUIRES_RECENT_LOGIN" to R.string.error_login_requires_recent_login,
            "ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL" to R.string.error_login_accounts_exits_with_different_credential,
            "ERROR_EMAIL_ALREADY_IN_USE" to  R.string.error_login_email_already_in_use,
            "ERROR_CREDENTIAL_ALREADY_IN_USE" to R.string.error_login_credential_already_in_use,
            "ERROR_USER_DISABLED" to R.string.error_login_user_disabled,
            "ERROR_USER_TOKEN_EXPIRED" to R.string.error_login_user_token_expired,
            "ERROR_USER_NOT_FOUND" to R.string.error_login_user_not_found,
            "ERROR_INVALID_USER_TOKEN" to R.string.error_login_invalid_user_token,
            "ERROR_OPERATION_NOT_ALLOWED" to R.string.error_login_operation_not_allowed,
            "ERROR_WEAK_PASSWORD" to R.string.error_login_password_is_weak)
    

    String resources (Feel free to change it according to your requirements)

      
        The custom token format is incorrect. Please check the documentation.
        The custom token corresponds to a different audience.
        The supplied auth credential is malformed or has expired.
        The email address is badly formatted.
        The password is invalid or the user does not have a password.
        The supplied credentials do not correspond to the previously signed in user.
        This operation is sensitive and requires recent authentication. Log in again before retrying this request.
        An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address.
        The email address is already in use by another account.
        This credential is already associated with a different user account.
        The user account has been disabled by an administrator.
        There is no user record corresponding to this identifier. The user may have been deleted.
        This operation is not allowed. You must enable this service in the console.
        The given password is invalid.
        The user\'s credential is no longer valid. The user must sign in again
        The user\'s credential is no longer valid. The user must sign in again.
    
    

提交回复
热议问题