获取“ FirebaseAuthInvalidCredentialsException:电子邮件地址格式错误。”使用Firebase进行身份验证时

时间:2019-07-09 09:15:18

标签: android firebase firebase-authentication

我正在尝试为我的应用创建登录名和注册页面。但是当我运行代码时,应用程序崩溃了。

这是错误:

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)

这是导致我认为麻烦的代码:

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
            }
        });
    }

我希望代码首先为已经存在的用户分析Editext视图,然后运行代码以注册用户。但这是我得到的错误。

2 个答案:

答案 0 :(得分:0)

为了避免崩溃,您需要在fetchSignInMethodsForEmail方法中检查task.isSuccessful()。

if (task.isSuccessful()) {    
}

代码示例,

    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());
        }
    }
});

,并在使用此android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()

发送到Firebase之前尝试验证电子邮件

答案 1 :(得分:0)

使用以下代码通过Google电子邮件进行身份验证:

firebaseauth = FirebaseAuth.getInstance();
        //auth state listener
        statelistener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser(); //firebaseAuth takes sign_in or sign_out params.

                if(user != null)
                {
//your code for if already logged in
}

 else
                {
                    startActivityForResult(
                            AuthUI.getInstance()
                                    .createSignInIntentBuilder()
                                    .setIsSmartLockEnabled(false)
                                    .setAvailableProviders(Arrays.asList(
                                            new AuthUI.IdpConfig.EmailBuilder().build(),
                                            new AuthUI.IdpConfig.GoogleBuilder().build()))
                                    .build(),
                            RC_SIGN_IN);
                }