I am developing a web app which uses Firebase Authentication and Real-time Database. The sign-in method is via Google and then if the user is new he/she will be ask if whether he/she is a teacher or student.
Teacher: can create classrooms and add students using their user.uid. Each classroom has a unique classroom id. Can view the list of their students.
Student: can view their classroom, and classmates.
So How do I code two different types of users in Firebase for my web app?
What will be the rules to use in the database?
[UPDATED]
You could try this the simple way:
Create a JSON object to store the users information in a field depends on the usage:
a field isTeacher of type boolean (assume you won't add other user type) or
a field userType of object contain all the feature types (isTeacher: boolean, isStudent: boolean, isHeadmaster:boolean, etc.) or
a field of userType of type string
Create a method or condition to check the field of the user document whether is true for a particular user type / compare string (depends which one you use) by using if or switch case
Under each case / condition, show the elements of the web according to their privilege.
After signed-in, before the page load, do the checking using the function created
Related
I have a custom claim "admin" attached to each user, carrying a boolean.
Now, in my frontend i am trying to generate a list of all admins.
For that i need to access all users who have said custom claim set to "true" and put them in an array, which i can then generate the list from.
The array should just look like the following:
const admins = ref([
{
uid: *uid of admin1*,
name: *name of admin1*,
},
{
uid: *uid of admin2*,
name: *name of admin2*,
},
...
])
So the following problems arise:
How do i access all users with said custom claim set to true, so that i can loop over them and populate my array?
Is this a case for a cloud function, so that it can not be manipulated?
I tried reading this Firebase documentation, however i could not make sense of it.
How do i access all users with said custom claim set to true, so that i can loop over them and populate my array?
There is no direct way to query users based on custom claims.
Is this a case for a cloud function?
Yes, you would have to list all users as in the documentation that you've shared and check for custom claims for each user separately.
It might be best to use a database like Firestore or Realtime Database and maintain a collection of all admins. Make sure this cannot be updated from client side directly using security rules.
I have an idea of doing a back office for my website. The problem is that, when I introduce a user who has, for example, two roles, I want the user to choose the role to work with, but I can't figure out how to do that. I've tried so different things such as putting the two roles separate with commas in MySQL and separating them in PHP but it didn't work.
let say you have a user table, and a user can have multiple role.
you can create a master table for role ( let say roles )
you can have a field on user table say user_roles -> it can have values like 1,2,4
note:- there should not be gaps between the numbers / comma
on the front-end you can take a mutiselect / tags UI element to input role for the user.
to fetch the role of a user at server side - you can use explode() + in_array() to check the role if exists
comma separated example
EDIT:-
It requires validation when adding roles ( ie: which user's role is allowed to select when already selected specific types )
For authorization, the user's roles should be added to the session :- this will help when allowing the user to do tasks he is allowed to do.
you can take a third table to maintain user's roles ( as suggested by #ADyson ), this will help if you want to join tables based on user + roles but you will need to join roles table for getting user data when starting session for the user.
I am relatively new to using mongoose and mongodb and trying to figure out what questions I should be asking myself when determining whether to create a new Schema and reference or embed by adding an array to an existing schema when there is a one to many relationship.
For example:
I have a User model. I need to keep track of User checkins (this will be a button the user presses on the UI to checking, it will call a POST route to my node api). I am debating on whether to embed an array of checkins to the User model or create a separate checkin Schema and reference User. I decided to create a checkin Schema that has a User reference by id. The reason for this is so I can query all checkins throughout the entire system easily (this is a requirement). I figured if I went the other direction and added an array of checkins to the User model (Schema), I would never be able to retrieve a master list of all checkins without looping through all of the Users in the system and compiling one. Additionally, now that I created a new Schema (model) I don't think I will be able to access a specific user's checkins from the User model, I would need to do a find on Checkins where userId=userId.
I am new to Mongo and Mongoose, so maybe there is a feature here that I might not be aware of.
The question is:
User (Schema)
checkins: [
{
...
},
],
or
User (Schema)
Checkin (Schema)
User
I am building a simple API and I have a registration end point where the user can register. I have a simple model with the username, email,name and password for the registration. When the user registers, I redirect them to the login page and once logged in, I have an edit profile endpoint accessible from the user's dashboard. I was wondering if there is a way in mongoose to dynamically add fields to the model that I already use for the registration. For instance I want to ad Job description as field to the model. Is this possible in mongoose or I have to set the fields when I create the schema and not have them required for registration?
Turn off the strict mode which will allow any key while inserting into db. https://mongoosejs.com/docs/guide.html#strict.
I am a bit new to meteor,I have been trying to create a login system for two different types of users say buyer and seller.When i use the useraccounts package and call the atform template {{>atform}} i can create accounts easily.But i want some added fields for the seller account registration(signup) say store name,phone no etc.,if i add those fields to atForm template they are reflected on buyer signup too,How can i get around this?is there any way to give two different looks to the atform template?
Since you segregating type of user, you can use https://github.com/alanning/meteor-roles.
From your question, what I understood is you navigate to atForm after you s ign-in. So while sign-up/registering you can provide selectbox to ask what kind of user he/she is and then just beforing creating user you can assign a role too.
For atForm, you can have a common form, but you can check role before showing certain fields.