How to catch a Firebase Auth specific exceptions

前端 未结 16 2343
慢半拍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:08

    I tried another solutions but didn't like them.

    What about this:

    if (!task.isSuccessful()) {
    
        Exception exc = task.getException();
    
        if (exc.getMessage().contains("The email address is badly formatted.")) {
            etUser.setError(getString(R.string.error_wrong_email));
            etUser.requestFocus();
        }
        else
        if (exc.getMessage().contains("There is no user record corresponding to this identifier. The user may have been deleted.")) {
            etUser.setError(getString(R.string.error_user_not_exist));
            etUser.requestFocus();
        }
        else
        if (exc.getMessage().contains("The password is invalid or the user does not have a password")) {
            etPass.setError(getString(R.string.error_wrong_password));
            etPass.requestFocus();
        }
    
    
        Log.w(TAG, "signInWithEmail:failed", task.getException());
    
    
        Toast.makeText(AuthActivity.this, R.string.auth_failed,
                Toast.LENGTH_SHORT).show();
    }
    

提交回复
热议问题