Firebase身份验证createUserWithEmailAndPassword在具有电子邮件变量的物理设备上失败

时间:2019-01-14 18:18:14

标签: java android firebase firebase-authentication

使用createUserWithEmailAndPassword时,有两个问题,

  1. 它可以在仿真器上运行,但不能在物理设备上的调试模式下运行

  2. 要使其在物理设备上工作,即使将其保存到变量中,我也可以对电子邮件部分的字符串进行硬编码。从EditText获取电子邮件时会中断,但我使用Log.d()在调用创建方法之前确认字符串完全相同。

这有效

    final String sEmail = "ExampleEmail@gmail.com";
            final String sPassword = password.getText().toString();
            final String sDisplayName = displayName.getText().toString();
            Log.d("Credentials:Email", sEmail);
            Log.d("Credentials:Password", sPassword);
            mAuth.createUserWithEmailAndPassword(sEmail,sPassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()){
                        FirebaseUser user = mAuth.getCurrentUser();
                        /*UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                                .setDisplayName(sDisplayName).build();
                        user.updateProfile(profileUpdates);
                        mAuth.signOut();*/
                        Intent intent = new Intent(MainActivity.this,UserLogin.class);
                        startActivity(intent);

                    }else{
                        Toast creationFailed = Toast.makeText(getApplicationContext(),"Creation Failed", Toast.LENGTH_SHORT);
                        creationFailed.show();
                    }

                }
            });

不是。

    final String sEmail = email.getText().toString();
            final String sPassword = password.getText().toString();
            final String sDisplayName = displayName.getText().toString();
            Log.d("Credentials:Email", sEmail);
            Log.d("Credentials:Password", sPassword);
            mAuth.createUserWithEmailAndPassword(sEmail,sPassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()){
                        FirebaseUser user = mAuth.getCurrentUser();
                        /*UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                                .setDisplayName(sDisplayName).build();
                        user.updateProfile(profileUpdates);
                        mAuth.signOut();*/
                        Intent intent = new Intent(MainActivity.this,UserLogin.class);
                        startActivity(intent);

                    }else{
                        Toast creationFailed = Toast.makeText(getApplicationContext(),"Creation Failed", Toast.LENGTH_SHORT);
                        creationFailed.show();
                    }

                }
            });

这是错误消息

    2019-01-14 13:44:48.298 3217-16541/? E/Volley: [1455] BasicNetwork.performRequest: Unexpected response code 400 for https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?alt=proto&key=AIzaSyCTfahJaTfOSAOdY7_pIN27-BGQgFlORnE

请注意,由于某种原因,第二个无效的设备将在模拟设备上运行。我希望第二个创建该帐户,但失败了。

1 个答案:

答案 0 :(得分:0)

我假设您输入的不是电子邮件类型。请记住,它需要电子邮件类型。

制定一种检查电子邮件有效的方法

   //Check email valid
    boolean isEmailValid(CharSequence email) {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }

然后将其应用于您的部分。

        //Check email valid
        if (!isEmailValid(email.getText().toString())) {
            email.setError("Not email type");
            email.requestFocus();
            return;
        }

关于您的吐司,如果这样放,也许会更好。因此,您可以知道遇到了什么错误。

else {             
      Toast.makeText(getApplicationContext(), "Fail: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
      }