Leonardo Simões
Leonardo Simões

Reputation: 23

Trying to set confirm password with firebase auth

I'm trying to program my first app and I'm using firebase auth email-password as my login method. On my sign up activity I want to have email, password and confirm password. I can't make the app check if password >= 6 characters and if password and confirm password are equals before the account is created. The app seems to check if password >= 6 but if thats true it creates the account without checking if password = confirm password. I also would like to display an error message saying that the username is already being used.

Here is my code

    private void signUpUser(String email, final String password) {
    auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (!task.isSuccessful())
                    {
                        if (password.length() < 6)
                            snackbar = Snackbar.make(activity_criar_conta, "Your password must have at least 6 characters.", Snackbar.LENGTH_SHORT);
                                    snackbar.show();

                        String pass2 = etConfirmarSenha.getText().toString();

                        if (!password.equals(pass2))
                        snackbar = Snackbar.make(activity_criar_conta, "Both password fields must be identic", Snackbar.LENGTH_SHORT);
                        snackbar.show();

                    }
                    else{
                        String emailuser = etCriarEmail.getText().toString();
                        snackbar = Snackbar.make(activity_criar_conta, "Your account was created with sucess: "+emailuser,Snackbar.LENGTH_SHORT);
                        snackbar.show();

Upvotes: 2

Views: 5646

Answers (1)

Ian Barber
Ian Barber

Reputation: 19970

You want to check your conditions before calling auth.createUserWithEmailAndPassword.

You've also got a missing { on your if statement, so its not doing what you expect. Should look something like this:

private void signUpUser(String email, final String password) {
   if (password.length() < 6) {
     snackbar = Snackbar.make(activity_criar_conta, "Your password must have at least 6 characters.", Snackbar.LENGTH_SHORT);
     snackbar.show();
     return;
   }
   String pass2 = etConfirmarSenha.getText().toString();
   if (!password.equals(pass2)) {
    snackbar = Snackbar.make(activity_criar_conta, "Both password fields must be identical", Snackbar.LENGTH_SHORT);
    snackbar.show();
    return;
   } 
   auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        String emailuser = etCriarEmail.getText().toString();
                        snackbar = Snackbar.make(activity_criar_conta, "Your account was created with sucess: "+emailuser,Snackbar.LENGTH_SHORT);
                        snackbar.show();

Upvotes: 2

Related Questions