Reputation: 7
I want to create three types of users:
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.
Upvotes: 0
Views: 185
Reputation: 4849
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.
Upvotes: 1