Mohammad Hossein Shojaeinia Mohammad Hossein Shojaeinia -4 years ago 146
Node.js Question

Role-Based login in nodejs and mongoose

I have an application with 2 roles, a User and a Photographer. the difference between these 2 models is a isAdmin field in User and a Photo[ ] in Photographer and a order[ ] in User. But I need only one login for both of them! Clearly, need different views and routs for them! how can I do this? If any guide, I would be appreciated! Thanks a lot!

The schemas are as below:

var userSchema = new Schema({
//some other fields, the same with photographer
isAdmin: { type: Boolean, default: false },
orders: [{
price: { type: Number, default: 0 }
{timestamps: true}

var photographerSchema = new Schema({
//some other fields, the same with user
photos: [{
title: { type: String, default: '' },
path: { type: String },
price: { type: Number, default: 0 },
isAppoved: { type: Boolean, default: false },
{timestamps: true}

Answer Source

You should rethink your architecture. Better solution would be to have 1 User model and 2 profile submodels. User model would contain the type of the user and fetch profile info(writer_profile/user_profile) from the submodels.

If you won't refactor, this will turn in to a mess pretty fast. Think about it, what happens when a User and a Writer registers with the same credentials? You will need to check that too.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download