简体   繁体   中英

Android Firebase create new user not working

I am hoping someone can help me. Below is my code for my create/register user. I am using Android Studio and Firebase. For some reason, the code is not creating the new user. I can manually add users to the database however, I cannot create new users when I run the emulator and test the login. The program gets stuck at running the progressDialog. When I remove the progressDialog, I get no response, so it appears that the program gets stuck when the createUserWithEmailAndPassword() gets called. I have enabled the Email and Password authentication in the Firebase console. I am not sure what the problem is and would appreciate any insights from someone more skilled at coding. Thank you all in advance.

public class RegisterPage extends AppCompatActivity implements View.OnClickListener{

//declaration of views (variables)
private Button btn_signup;
private EditText txt_firstname;
private EditText txt_lastname;
private EditText txt_email_signup;
private EditText txt_username;
private EditText txt_password_signup;
private EditText txt_passwordConfirm;

private ProgressDialog progressDialog;
private FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthStateListener;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register_page);

    //initialization of view (assign id's)
    progressDialog = new ProgressDialog(this);
    btn_signup = (Button) findViewById(R.id.btn_signup);
    txt_firstname = (EditText) findViewById(R.id.txt_firstname);
    txt_lastname = (EditText) findViewById(R.id.txt_lastname);
    txt_email_signup = (EditText) findViewById(R.id.txt_email_signup);
    txt_username = (EditText) findViewById(R.id.txt_username);
    txt_password_signup = (EditText) findViewById(R.id.txt_password_signup);
    txt_passwordConfirm = (EditText)findViewById(R.id.txt_passwordConfirm);

    //assign database instances
    mAuth = FirebaseAuth.getInstance();
    mAuthStateListener = new FirebaseAuth.AuthStateListener(){
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if(user != null) {
            }
            else{
                startActivity(new Intent(RegisterPage.this, UserMainPage.class));
            }
        }
    };


    //set the listener for the click event
    btn_signup.setOnClickListener(this);

}

//function to register user
private void registerUser(){
    //get user input
    String email = txt_email_signup.getText().toString().trim();
    String password = txt_password_signup.getText().toString().trim();
    String confirm = txt_passwordConfirm.getText().toString().trim();
    String firstname = txt_firstname.getText().toString().trim();
    String lastname = txt_lastname.getText().toString().trim();
    String username = txt_username.getText().toString().trim();

        //check if stings are empty using TextUtils
        if(TextUtils.isEmpty(firstname)){ //email is empty
            Toast.makeText(this, "Please enter firstname", Toast.LENGTH_SHORT).show();
            //stop further execution
            return;

        }
        if(TextUtils.isEmpty(lastname)){ //email is empty
            Toast.makeText(this, "Please enter lastname", Toast.LENGTH_SHORT).show();
            //stop further execution
            return;

        }
        if(TextUtils.isEmpty(username)){ //email is empty
            Toast.makeText(this, "Please enter a username", Toast.LENGTH_SHORT).show();
            //stop further execution
            return;

        }
        if(TextUtils.isEmpty(email)){ //email is empty
            Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
            //stop further execution
            return;

        }
        if(TextUtils.isEmpty(password)){ //password is empty
            Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
            //stop further execution
            return;
        }
        if(!password.equals(confirm)){
            Toast.makeText(this, "Your passwords do not match", Toast.LENGTH_SHORT).show();
            //stop further execution
            return;
        }


            //if validations are okay
            //we will show a progressDialog as we create user account
            progressDialog.setMessage("Creating account...");
            progressDialog.show();



    //register user in firebase database
    mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {

                            @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                           progressDialog.dismiss();
                                if (task.isSuccessful()){
                                // user registered, start profile activity
                                Toast.makeText(RegisterPage.this,"Account Created",Toast.LENGTH_LONG).show();

                                finish();
                                startActivity(new Intent(getApplicationContext(), UserMainPage.class));
                            }
                            else{
                                Toast.makeText(RegisterPage.this,"Could not create account. Please try again",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

}


@Override
public void onClick(View view){
    if(view == btn_signup){
        //if signup button clicked call function register user
        registerUser();
    }
}


/* @Override
   protected void onStart(){
    super.onStart();
    mAuth.addAuthStateListener(mAuthStateListener);
  }

 @Override
 protected void onStop(){
    super.onStop();
    mAuth.removeAuthStateListener(mAuthStateListener);
 }*/
}

This is happening because you are not creating a user at all. So in your onComplete method you need to get the Firebase current user. So if (task.isSuccessful() get the user like this:

mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
       progressDialog.dismiss();
        if (task.isSuccessful()){
        FirebaseUser user = mAuth.getCurrentUser(); //You Firebase user
        // user registered, start profile activity
        Toast.makeText(RegisterPage.this,"Account Created",Toast.LENGTH_LONG).show();

        finish();
        startActivity(new Intent(getApplicationContext(), UserMainPage.class));
        }
        else{
        Toast.makeText(RegisterPage.this,"Could not create account. Please try again",Toast.LENGTH_SHORT).show();
        }
    }
    }); 

Hope it helps.

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