0

In my android app, there are 2 types of accounts (Admin and User). The Admin can create a User account. The issue is that after I create User account from admin, the FirebaseAuth changes to that new User which I dont want.

The use case is: Admin creates a User account. The User would login from his own device later. (Both admin and User belongs to same Company, and I have opted for this approach for ease of use)

So is there a way to create an account in firebase and FirebaseAuth is NOT changed?

Thanks.

Ive tried to relogin the Admin after creating Uesr account, but in the meantime all listeners get fired (onCancelled()), due to security rules. So this approach doesnt work

droidbee
  • 134
  • 2
  • 16
  • 1
    "https://stackoverflow.com/questions/54648880/can-i-send-value-with-request-to-firestore-rules/54653648#54653648" See https://stackoverflow.com/questions/37517208/firebase-kicks-out-current-user – Frank van Puffelen Jul 07 '19 at 19:27
  • But why do you want to admin to **create** the account of the new user? Why not let them add the new user's email address (or other identifying properties) to a database as a whitelisted user? Then when a user signs in, you grant them access to the app's data based on them being signed in **and** being whitelisted by an admin. – Frank van Puffelen Jul 07 '19 at 19:28
  • I chose this approach for simplicity. My end Users (non-admin) are not tech savvy and usually illiterate, and are only reqd to punch in some numbers, hence admin simplifies their account creation by setting up their accounts. Your idea provides an alternate method. I will try that. – droidbee Jul 08 '19 at 05:08
  • 1
    If you stick to your current approach, the linked question shows how to create a new account without signing out the current user. If that doesn't work for you, show what you tried so we can try to help troubleshoot. – Frank van Puffelen Jul 08 '19 at 14:05
  • That worked! But stuck at another issue after signup is done. I've updated the question. – droidbee Jul 10 '19 at 05:08
  • If it's another problem, open a new question. Be sure to include the minimal **complete** code that is needed to reproduce the issue there, as right now I don't immediately see where `userid` comes from. – Frank van Puffelen Jul 10 '19 at 14:00

1 Answers1

1

I have been through the same problem a while ago implementing a feature for a client.

How I solved it.

Simply storing the admin UID into shared preferences, so, each time I created a new user within the admin, I assigned that user the UID of the admin who created it.

Also, if you log in with another admin, just overwrite the current UID that is in sharedprefs with the current admin uid that logged in.

Doing this you can keep creating and storing users into the admin ID

If I find the code of that implementation, I will be posting it here

EDIT

Found a little bit of the code that can help you understand

for example, to create a new user

public void cargarPacienteFirebase(String email, String password, final String nombre, final String apellido, final Dialog mDialog) {

        SharedPreferences prefs = mContext.getSharedPreferences("ADMINUID", Context.MODE_PRIVATE);
        final String adminUID = prefs.getString("adminuid", "defaultValue");

        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();

        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();

                    SharedPreferences settings = mContext.getSharedPreferences("PACIENTEUID", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("pacienteuid", pacienteID);
                    editor.apply();


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


                    Map<String, Object> userLookUp = new HashMap<>();
                    userLookUp.put(pacienteID, adminUID);
                    mDatabase.child("Test").child("userLookup").updateChildren(userLookUp);


                    Toast.makeText(mContext, "Succefully added a new user", Toast.LENGTH_SHORT).show();
                    mDialog.dismiss();

                } else {

                    pd.dismiss();

                    Toast.makeText(mContext, "E-mail or password invalid",
                            Toast.LENGTH_SHORT).show();
                }

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

                Toast.makeText(mContext, "A problem has ocured: " + e.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });

There is something in Spanish, this is an old implementation that I had in a repo, I'm sure it will help you.

As you can see there, userLookup was a table of users with their current UID, so I can match that specific user id with the one in that table and get all user information.

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
  • After you do mAuth.createUserWithEmailAndPassword(email, password), FirebaseAuth.getInstance() would change to the newly created user and all firebase hooks in ur code will fire and Permission Denied error would result as the new user has no rights on the data the Admin had. That exactly is my issue – droidbee Jul 08 '19 at 05:10