Home > Net >  I need to create 3 types of users
I need to create 3 types of users

Time:03-14

Hello tanks for you time,

I want to create three types of users: 1.is admin (the person that crated a question) 2.is Answer user 3.is additional user that can ask question to the 2 user.

I want to try for backend, build it with FireBase But i cant round my head how to build the Data structure, because I’m used to build my own Apis and I see that firebase doesn’t have an option of making schemes for user types.

And i will use for client , react native I can’t figure out what function I need to write so it will recognise the 3 types of users, and everything i found on the web it’s in old structure of components of classes and I’m trying to make it with the new method,

So if you can help me wrap my head around this because I don’t have a lot of experience of solving problems like this.

Its for a case study.

Tanks have a great week

CodePudding user response:

In common applications, different user types have different roles and privileges.

In your case study. Create a user with one of the ADMIN, ASK or ANSWER roles.

Apart from that user role, you can grant permissions and privileges to that user later in your app logic.

import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
import { doc, setDoc, getFirestore } from "firebase/firestore";

const db = getFirestore();

async function saveUser(userId, { email, firstName, lastName, role }) {
  await setDoc(doc(db, "users", userId), { email, firstName, lastName, role });
}

/** User role should be either ADMIN,ASK or ANSWER **/
function signup({ email, password, firstName, lastName, role }) {
  const auth = getAuth();
  createUserWithEmailAndPassword(auth, email, password)
    .then((userCredential) => {
      // Signed in
      const user = userCredential.user;
      const userId = user.uid;

      // Save new user to database
      saveUser(userId, { email, firstName, lastName, role });
      // ...
    })
    .catch((error) => {
      const errorCode = error.code;
      const errorMessage = error.message;
      // ..
    });
}


Apart from that user role, you can grant permissions and privileges to that user later in your app business logic.

  • Related