How to catch a Firebase Auth specific exceptions

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

    To catch a firebase Exception is easy, you should add .addOnFailureListener after you add .addOnCompleteListener like this:

     private void login_user(String email, String password) {
    
        mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task task) {
               if(task.isSuccessful()){
                   Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                   startActivity(intent);
                   finish();
               }if(!task.isSuccessful()){
    
    
                    // To know The Excepton 
                    //Toast.makeText(LoginActivity.this, ""+task.getException(), Toast.LENGTH_LONG).show();
    
               }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                if( e instanceof FirebaseAuthInvalidUserException){
                    Toast.makeText(LoginActivity.this, "This User Not Found , Create A New Account", Toast.LENGTH_SHORT).show();
                }
                if( e instanceof FirebaseAuthInvalidCredentialsException){
                    Toast.makeText(LoginActivity.this, "The Password Is Invalid, Please Try Valid Password", Toast.LENGTH_SHORT).show();
                }
                if(e instanceof FirebaseNetworkException){
                    Toast.makeText(LoginActivity.this, "Please Check Your Connection", Toast.LENGTH_SHORT).show();
                }
            }
        });
    

提交回复
热议问题