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