Gastón Saillén
Gastón Saillén

Reputation: 13129

FirebaseAuth is changing my current Auth UID

I'm making an app that registers a user which I'm creating manually from Firebase Authentication panel. After I login with that user my Auth is equal to the auth I have on Firebase

enter image description here

So far that login is working nice since after I login with it gaves to my mAuth.getCurrentUser().getUid(); that id.

Now, the problem is the following, in the next Activity I need to create users with a dialog that needs to be stored inside that UID, now I managed to do that in this way.

 final UserModel userModel = new UserModel();
        userModel.setNombre(nombre);
        userModel.setApellido(apellido);
        final ProgressDialog pd = new ProgressDialog(mContext);
        pd.setMessage("Estamos cargando su paciente...");
        pd.setCancelable(false);
        pd.show();
        final String adminUID = mAuth.getCurrentUser().getUid();
        mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    pd.dismiss();
                    final String pacienteID = task.getResult().getUser().getUid();
                    mDatabase.child("Bord").child(adminUID).child("pacientes").setValue(pacienteID).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {

                            Map<String,Object> mapUsuario = new HashMap<>();
                            mapUsuario.put("nombre",userModel.getNombre());
                            mapUsuario.put("apellido",userModel.getApellido());
                            mapUsuario.put("pregunta",1);
                            mDatabase.child("Bord").child(adminUID).child("pacientes").child(pacienteID).updateChildren(mapUsuario);
                        }
                    });

                    Toast.makeText(mContext, "Su paciente se cargo con exito", Toast.LENGTH_SHORT).show();
                    mDialog.dismiss();

                } else {

                    pd.dismiss();
                    Log.i("Tags", "Tags signInWithEmail:failure" + task.getException());
                    Toast.makeText(mContext, "Su contraseña o email es incorrecta.",
                            Toast.LENGTH_SHORT).show();
                }

This line final String adminUID = mAuth.getCurrentUser().getUid(); before the createUserWithEmailAndPassword method (from FirebaseAuth to create a new user) is giving to me the mAuth of the first login I made with the user id from the user I created in Firebase console, but after createUserWithEmailAndPassword I can store that user one time inside my node.

enter image description here

This is how it should work. But the problem relies the second time I press that dialog to add another paciente inside my node.

enter image description here

It seems like createUserWithEmailAndPassword logged me into firebase after I created my first user, and then, when I tried to add a new one it used the UID from that last user.

So my question is

Is there any way to prevent createUserWithEmailAndPassword to auto login or change my current userID which I logged in first time ?

Upvotes: 2

Views: 1583

Answers (1)

Ricardo Smania
Ricardo Smania

Reputation: 3139

This seems to be by design. But I see two possible solutions:

  1. Create a second Firebase client instance, connecting to the same project, and use that to create the new users. Details on how to do that can be found here: https://firebase.google.com/docs/configure/

or

  1. Create a Cloud function that receives the email and password and creates the user using the admin SDK, and call that from your app using HttpsCallable. You can also get the current user there and only allow the request if the user is an admin. More details: https://firebase.google.com/docs/functions/callable

Upvotes: 1

Related Questions