0

Im trying to build an app where only current users can create accounts for new users.I have two types of users Doctors and Patients. Only the Doctors will be able to create accounts for their Patients. So far Iv created a Firebase database and it seemed to be working but now it has stopped.The Patients are now being authenticated but their information is not being added to the database. I think the issue is with trying to create a new user while a current user is signed in.Is there away to allow a user to create a new user without signing in as the newly created user?

This code Creates Patient Accounts and adds them to Firebase

 public class PatientRegister extends AppCompatActivity {
 EditText FirstName,LastName,PatientEmail,PatientPassword;
 Button CreateAccount;
 FirebaseAuth mAuth2;
 AuthStateListener firebaseAuthStateListener;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_patient_register);

    FirstName = findViewById(R.id.fName);
    LastName = findViewById(R.id.lName);
    PatientEmail = findViewById(R.id.patientEmail);
    PatientPassword = findViewById(R.id.patientPassword);
    CreateAccount = findViewById(R.id.addPatient);
    mAuth2 = FirebaseAuth.getInstance();

     firebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
         @Override
         public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
             final FirebaseUser patientAcc = FirebaseAuth.getInstance().getCurrentUser();
             if (patientAcc != null){

             } } };
    CreateAccount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String fname = FirstName.getText().toString();
            final String lname = LastName.getText().toString();
            final String patientemail = PatientEmail.getText().toString();
            final String patientpassword = PatientPassword.getText().toString();

            startActivity(new Intent(getApplicationContext(),PatientAccount.class));

            mAuth2.createUserWithEmailAndPassword(patientemail,patientpassword).addOnCompleteListener(PatientRegister.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (!task.isSuccessful()) {
                        Toast.makeText(PatientRegister.this, "sign up error", Toast.LENGTH_SHORT).show();
                    } else {
                        String patientId = mAuth2.getCurrentUser().getUid();
                        DatabaseReference PatientDataBase = FirebaseDatabase.getInstance().getReference().child("Patient").child(patientId);
                        Map<String, Object> patient = new HashMap<>();
                        patient.put("Last Name", lname);
                        patient.put("First Name", fname);
                        patient.put("Patient Email", patientemail);
                        PatientDataBase.updateChildren(patient);
                    } }}); } }); }
 @Override
protected void onStart() {
    super.onStart();
    mAuth2.addAuthStateListener(firebaseAuthStateListener);
}

@Override
protected void onStop() {
    super.onStop();
    mAuth2.removeAuthStateListener(firebaseAuthStateListener);
  }}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • See https://stackoverflow.com/questions/37517208/firebase-kicks-out-current-user, although I'd now recommend taking the approach Doug outlines instead of what André shows in the accepted answer. – Frank van Puffelen Mar 05 '20 at 02:02

1 Answers1

0

It's not possible in a client app. Firebase Authentication only allows a single user to be signed in at any given time. When you call createUserWithEmailAndPassword, that effectively creates the new user and signs them in at the same time. The original user is then immediately signed out.

If you want a user to be able to properly create another user, you will need to use the Firebase Admin SDK on a backend you control. It can manage user account, including creating new accounts. Your app will have to invoke this backend somehow.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441