How to catch a Firebase Auth specific exceptions

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

    In the past we used the getErrorCode() to get the type of error and fail gracefully. With the newer versions of the api, getErrorCode() is deprecated. We should use response.getError().getErrorCode() instead

    com.firebase.ui.auth.IdpResponse
    @Deprecated 
    public int getErrorCode()
    Get the error code for a failed sign in
    
    Deprecated use getError() instead
    

    So for e.g.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
                    super.onActivityResult(requestCode, resultCode, data);
    
                    if (requestCode == RC_SIGN_IN) {
                        IdpResponse response = IdpResponse.fromResultIntent(data);
    
                        // Successfully signed in
                        if (resultCode == RESULT_OK) {
                            //dbHandler = DBMS.getInstance(this);
    
                            FirebaseAuth auth = FirebaseAuth.getInstance();
                            FirebaseUser user = auth.getCurrentUser();
                            FirebaseUserMetadata metadata = auth.getCurrentUser().getMetadata();
    
                            // initialize profile first
                            if (metadata.getCreationTimestamp() == metadata.getLastSignInTimestamp()) {
    
    
    
                                //start main activity after profile setup
                                startActivity(new Intent(this, MainActivity.class));
                                return;
                            } else {
                                // This is an existing user
                                // show them a welcome back screen.
    
                                startActivity(new Intent(this, MainActivity.class));
                                return;
                            }
                        } else {
                            // Sign in failed
                            // check response for error code
                            if (response == null) {
                                // User pressed back button
                                showSnackbar(R.string.sign_in_cancelled);
                                return;
                            }
    
                            if (response.getError().getErrorCode() == ErrorCodes.NO_NETWORK) {
                                showSnackbar(R.string.no_internet_connection);
                                return;
                            }
    
                            if (response.getError().getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
                                showSnackbar(R.string.unknown_error);
                                return;
                            }
                        }
                        showSnackbar(R.string.unknown_sign_in_response);
                    }
                }
    

提交回复
热议问题