Im still new to programming so please go easy on me lol. Im planning on creating 2 types of users in my project a normal user and an Admin user. I was able to implement a local strategy for normal users and its already working but I cant seem to visualize or grasp how i will include another type of user. do i have to create a different collection in mongo just for admin users? please enlighten me on this subject.
Filipe Picoito
Full Stack Developer
I'd assume that Normal user and Admin user just change in permissions mostly right? So they would be the same model, except they would have a property which would separate the two:
const userSchema = new Schema({ name: String, email: String, isAdmin: Boolean })If you wanted to complicate things a bit more, you could have roles:
const userSchema = new Schema({ name: String, email: String, roles: [String] // ["admin", "moderator"] })If you wanted to further increase the complexity, you could use a schema instead of a String for the roles
const roleSchema = new Schema({ name: String // more properties here }) const user = new Schema({ name: String, email: String, roles: [roleSchema] })So either way, once you had a hold of your user document you could just check for the property which dictated if it was an admin or not.
const user = await UserModel.findById(userId).exec() // don't continue if not the admin if (!user.isAdmin) return;The one thing I'd like you to retain is that when you're thinking about database you would want to figure out "what information is the same?"
So regarding Admin and User, what information does the User model hold for both of them? Etc...
If it were the case you really wanted to separate both, you would probably have a User model, which held the schema for the information that was the same for both of them, and then a RegularUser and AdminUser schemas.
const userSchema = new Schema({ name: String, email: String }) const regularUserSchema = new Schema({ user: userSchema, // regular-only properties }) const adminUserSchema = new Schema({ user: userSchema, // admin-only properties })Something along those lines I believe.
Please bare in mind this is a very simple example!