Ad

Getting "FirebaseAuthInvalidCredentialsException: The Email Address Is Badly Formatted." When Authenticating With Firebase

I am trying to create a login and a signup page for my app. But when I run the code the app crashes.

This is the error:

main
    Process: in.testprepindia, PID: 2884
    com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The email address is badly formatted.
        at com.google.android.gms.tasks.zzu.getResult(Unknown Source:15)
        at in.testprepindia.RegistrationActivity$3.onComplete(RegistrationActivity.java:190)
        at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7076)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
     Caused by: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The email address is badly formatted.
        at com.google.firebase.auth.api.internal.zzdr.zzb(Unknown Source:30)
        at com.google.firebase.auth.api.internal.zzeu.zza(Unknown Source:16)
        at com.google.firebase.auth.api.internal.zzen.zzc(Unknown Source:33)
        at com.google.firebase.auth.api.internal.zzep.onFailure(Unknown Source:49)
        at com.google.firebase.auth.api.internal.zzdx.dispatchTransaction(Unknown Source:18)
        at com.google.android.gms.internal.firebase_auth.zza.onTransact(Unknown Source:13)
        at android.os.Binder.execTransact(Binder.java:739)

This is the code causing the trouble I think:

private void compareData() {
        firebaseAuth.fetchSignInMethodsForEmail(newEmail.getText().toString()).addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
            @Override
            public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
                boolean check = !task.getResult().getSignInMethods().isEmpty();

                if(!check){
                    String user_email = newEmail.getText().toString().trim();
                    String user_password = newPassword.getText().toString().trim();

                    // firebase authentication started

                    firebaseAuth.createUserWithEmailAndPassword(user_email, user_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(RegistrationActivity.this, "Successful", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(RegistrationActivity.this, MainActivity.class));

                            }
                            else{
                                Toast.makeText(RegistrationActivity.this, "unsuccessful", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    // firebase authentication ended

                } //if Ended
                //else started
                else {
                    Toast.makeText(RegistrationActivity.this, "Email already present", Toast.LENGTH_SHORT).show();
                }
                //else ended
            }
        });
    }

I expected the code to first analyse the Editext view for already present users and then run the code to register the user. But this is the error I am getting.

Ad

Answer

In order to avoid the crash, you need to check task.isSuccessful() in your fetchSignInMethodsForEmail method.

if (task.isSuccessful()) {    
}

Code example,

    firebaseAuth.fetchSignInMethodsForEmail(newEmail.getText().toString()).addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
    @Override
    public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
        if (task.isSuccessful()) {
            //your code
            boolean check = !task.getResult().getSignInMethods().isEmpty();
            // Your code
            /*if (!check) {
                String user_email = newEmail.getText().toString().trim();
                String user_password = newPassword.getText().toString().trim();
                .
                .
                .

                */
        } else {
            Log.e(TAG, "Error getting sign in methods for user", task.getException());
        }
    }
});

and try to validate email before sending to firebase using this android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()

Ad
source: stackoverflow.com
Ad