1

I am trying to create a web application with ReactJs and firebase. I want to know how to create three users with different roles like doctor, patient, pharmacist.

My Signup.js file

    if (pass.value != cpass.value) {
  alert("Password dont match")
}
else {
  try {
    var e = email.value
    const ref = db.collection("doctor")
    setLoading(true)
    await app
      .auth()
      .createUserWithEmailAndPassword(email.value, pass.value);
    ref.doc(e).set({
      "name": uname.value,
      "dob": dob.value,
      "email": email.value,
      "phno": phno.value,
      "regno": regno.value,
      "regyear": regyear.value,
      "gender": gender.value,
      "addr": addr.value,
      "pincode": pin.value,
      "city": city.value,
      "state": state.value,
      "bloodtype": bloodtype.value,
      "speciality": speciality.value,


    }).then(() => {
      console.log("Document successfully written!");
    });
    db.collection("users").doc(email.value).set({
      "email":email.value,
      "userType":"doctor"
    })
Bijin Abraham
  • 1,709
  • 2
  • 11
  • 25
fury
  • 13
  • 3
  • Does this answer your question? [How to create different user groups in Firebase?](https://stackoverflow.com/questions/48255622/how-to-create-different-user-groups-in-firebase) – Roger May 12 '21 at 14:12

1 Answers1

1

You have set the collection to doctors in the following code snippet:

const ref = db.collection("doctor")

If you want to add a different user to a different firebase collection you just need to replace the collection name:

const ref = db.collection("patient")

Fabio
  • 160
  • 2
  • 12
  • i have done that i have currently made 3 signups and 3 login forms but i want only one login.This signup.js is for doctor i have two more signup.js .For patient it is db.collection("patient") and for pharmcist its db.collection("pharmacist").I want to know if there is a firebase function which will allow me to give roles so that when the user logs in i can just have one login form with the authenticate function and route them to the homepage using their roles – fury May 07 '21 at 09:11
  • const handleLogin = useCallback( async event => { event.preventDefault(); const { email, pass } = event.target.elements; try { await app .auth() .signInWithEmailAndPassword(email.value, pass.value); hist.push(Routes.PatientDashboardOverview.path); } catch (error) { alert(error); } }, [history] ); – fury May 07 '21 at 09:14
  • Then you need to pass the user type into the handleLogin arguments: const handleLogin = (type) => { event.preventDefault(); const { email, pass } = event.target.elements; .....} and call handleLogin with the selected user type as a string. – Fabio May 07 '21 at 09:14