1

I'm making a signup activity for an app and something weird is happening can you help me?

this is the code for signup some on and it works if I comment the signOut function but I need it to signout after saving the data in the FirebaseAuth and the Firestore.

I don't get even the logs.

public void registUser() {

    mAuth = FirebaseAuth.getInstance();
    mAuth.createUserWithEmailAndPassword(user.getEmail(), user.getPassword()).addOnCompleteListener(SignUpUserActivity.this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {

                Toast.makeText(SignUpUserActivity.this, "Sucesso", Toast.LENGTH_SHORT).show();
                // Sign in success, update UI with the signed-in user's information
                //FirebaseUser user = mAuth.getCurrentUser();
                user.setId(task.getResult().getUser().getUid());

                user.save();

                mAuth.signOut();
                finish();



            } else {
                String erro = "";
                try {
                    throw task.getException();


                } catch (FirebaseAuthWeakPasswordException e) {
                    erro = "Digite uma senha mais forte";
                } catch (FirebaseAuthInvalidCredentialsException e) {
                    erro = "Email invalido";
                } catch (FirebaseAuthUserCollisionException e) {
                    erro = "Email Registado";
                } catch (Exception e) {
                    erro = "Erro generico";
                    e.printStackTrace();
                }
                Toast.makeText(SignUpUserActivity.this, erro, Toast.LENGTH_SHORT).show();

            }

        }

    });
}

I think its because it takes to long to save things in the database. I have no problems with the Auth part.

Thank you.

this is the save function.

 public void save(){


    DocumentReference reference = FirebaseFirestore.getInstance().document("/Clinicas/" + clinic + "/Profissionais/" + id + "/");
    Map<String, Object> dataToSave = new HashMap<String, Object>();
    dataToSave.put("Name", name);
    dataToSave.put("email", email);
    dataToSave.put("clinic", clinic);
    dataToSave.put("Job", job);
    reference.set(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.d("artur", "Saved");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.w("artur", "Not Saved",e);
        }
    });
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Why would you sing-in the user and then when everything is complete to sign him out? – Alex Mamo May 29 '18 at 15:49
  • I'm not sign him In, I'm signing him up. after that I need him to go the first activity. – Ricardo Rodrigues May 29 '18 at 15:52
  • That's good but why are you using `mAuth.signOut();` inside the `onComplete()` method? What you are doing, basically means, once you have succeeded with the authentication process, sign out. Is this what you want? – Alex Mamo May 29 '18 at 15:54
  • https://stackoverflow.com/a/47021042/9025311 –  May 29 '18 at 15:56
  • Yes, I want it to signUp save the data in the Firestore and the FirebaseAuth and after that signOut and finish the activity. and it works if I don't signOut. I also saw the same code but with firebase realtime database and it works fine. – Ricardo Rodrigues May 29 '18 at 15:57

2 Answers2

2

To sovle this, please use the following code:

user.setId(task.getResult().getUser().getUid());
userRef.set(user).addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        mAuth.signOut();
    }
});

When you sign-out, you need to be sure that your write operation is done and this can only be achieved using addOnSuccessListener.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0
public void save(){


    DocumentReference reference = FirebaseFirestore.getInstance().document("/Clinicas/" + clinic + "/Profissionais/" + id + "/");
    Map<String, Object> dataToSave = new HashMap<String, Object>();
    dataToSave.put("Name", name);
    dataToSave.put("email", email);
    dataToSave.put("clinic", clinic);
    dataToSave.put("Job", job);
    reference.set(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {

        @Override
        public void onSuccess(Void aVoid) {
            Log.d("artur", "Saved");
            mAuth = FirebaseAuth.getInstance();
            mAuth.signOut();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.w("artur", "Not Saved",e);
        }
    });




}