I'm using two schemas for users. One that contains the password/salt, one that doesn't for returning to the front end. When I use the model that uses the schema WITHOUT the password, it still returns the password :/
Generic User (For sending to the client)
module.exports = {
username: String,
email: String,
firstName: String,
lastName: String,
createdOn: Date,
updatedOn: Date,
scopes: [String]
}
Auth User (for creating/updating/authenticating users)
module.exports = {
username: String,
email: String,
password: String,
salt: String,
firstName: String,
lastName: String,
createdOn: Date,
updatedOn: Date,
scopes: [String]
}
Creating the models with
var modelInstance = mongoose.model("authUser", authUserSchema, 'users')
(in a different file)
var modelInstance = mongoose.model("user", userSchema, 'users')
modelInstance is exported with module.exports = modelInstance;
Update This question answers mine. How to protect the password field in Mongoose/MongoDB so it won't return in a query when I populate collections?
users
It is going to return you the fields whichever model u execute the query from. To restrict the field there are various ways prevent select thepassword
field when you query from Model ofuserSchema
. Also assuming that you are following One schema/model per file structure in node – ambianBeing