简体   繁体   中英

Android: Email account registration unsuccessful for firebase

I write a very simple app to test firebase working. I add this dependencies to my build.gradle(App):

implementation 'com.google.firebase:firebase-core:9.6.0'
compile 'com.firebaseui:firebase-ui-auth:4.2.0'
compile 'com.firebaseui:firebase-ui-database:4.2.0'
compile "com.google.firebase:firebase-messaging:17.3.0"

and this plugin at the end of file:

apply plugin: 'com.google.gms.google-services'

and classpath 'com.google.gms:google-services:4.0.1' to my build.gradle(project) . In mainActivity I add these code to signup/login user with using firebase Authentication:

public class MainActivity extends AppCompatActivity {
    public final int SIGN_IN_REQUEST_CODE=123;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(FirebaseAuth.getInstance().getCurrentUser() == null) {
            // Start sign in/sign up activity
            startActivityForResult(
                    AuthUI.getInstance()
                            .createSignInIntentBuilder()
                            .build(),
                    SIGN_IN_REQUEST_CODE
            );
        }
        else {
            // User is already signed in. Therefore, display
            // a welcome Toast
            Toast.makeText(this,
                    "Welcome " + FirebaseAuth.getInstance()
                            .getCurrentUser()
                            .getDisplayName(),
                    Toast.LENGTH_LONG)
                    .show();

            // Load chat room contents
            displayChatMessages();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == SIGN_IN_REQUEST_CODE) {
            if(resultCode == RESULT_OK) {
                Toast.makeText(this,
                        "Successfully signed in. Welcome!",
                        Toast.LENGTH_LONG)
                        .show();
                displayChatMessages();
            } else {
                Toast.makeText(this,
                        "We couldn't sign you in. Please try again later.",
                        Toast.LENGTH_LONG)
                        .show();

                // Close the app
                finish();
            }
        }
    }

    private void displayChatMessages() {
    }
}

I debug the code the if(FirebaseAuth.getInstance().getCurrentUser() == null) condition performed but the onActivityResult never runnig . When I tested on mobile and fill the email, username and password and I pressed the save button it shows the "Email account registration unsuccessful" error. Where is my wrong? thanks for any help.

try this code

  reg_button.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           String email = reg_email.getText().toString();
           String password = reg_password.getText().toString();
           String confirm_pass =confirm_password.getText().toString();

           if (!TextUtils.isEmpty(email) &&  !TextUtils.isEmpty(password)  && !TextUtils.isEmpty(confirm_pass))
           {

               if (password.equals(confirm_pass))
               {
                   reg_progress_bar.setVisibility(View.VISIBLE);
                   mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                       @Override
                       public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful())
                            {
                               Intent settingActivity = new Intent(getApplicationContext(),ProfileActivity.class);
                               startActivity(settingActivity);
                               finish();
                            }

                            else
                            {
                                String errorMessage = task.getException().getMessage();
                                Toast.makeText(RegisterActivity.this, "error" +errorMessage, Toast.LENGTH_SHORT).show();
                            }
                           reg_progress_bar.setVisibility(View.INVISIBLE);


                       }
                   });
               }
               else
               {
                   Toast.makeText(RegisterActivity.this, "password and confirm password does not match", Toast.LENGTH_SHORT).show();
               }

           }



       }
   });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM