0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
yukhei
  • 7
  • 2
  • Sorry, but if you want advices, some people might give some to you, but here we mainly work with explanation of confusing behaviour, or problems in programming that don't have a clear solution. Programming is all about logic, so you'll need to learn programming first, and then transform the pseudocode into actual code. – Rafaelplayerxd YT Mar 14 '22 at 10:31
  • 2
    Your question is very broad, but few suggestions: 1) You can use firebase [custom claims](https://firebase.google.com/docs/auth/admin/custom-claims) to define the user roles. 2) The client side code can check the claims to modify the user experience. 3) For important things, check the claims on the server side. Eg, use [firestore security rules](https://firebase.google.com/docs/firestore/security/get-started) to limit access to the database, or use a firebase function to check for the right permissions before doing sensitive actions. – Nicholas Tower Mar 14 '22 at 10:49
  • Thanks @NicholasTower , i checked the links, that was exactly the thing I was missing in my logic to understand how to make the flow work . – yukhei Mar 14 '22 at 10:58

1 Answers1

1

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.

Fiston Emmanuel
  • 4,242
  • 1
  • 8
  • 14