Problem
The current User schema (models/User.js) works functionally but has a few gaps
that should be fixed before production use, mainly around validation and
sensitive data exposure.
Proposed Improvements
- Email validation — add regex check so invalid emails can't be saved.
- Password exposure — add
select: false to password field so it
doesn't accidentally leak in API responses (e.g. User.find()).
- Reset token exposure — add
select: false to resetPasswordToken and
resetPasswordExpires since these are sensitive fields too.
- googleId / githubId uniqueness — add a sparse unique index so OAuth IDs
stay unique without breaking local users who don't have these fields.
- Response sanitization — add a
toJSON transform to automatically strip
password, resetPasswordToken, resetPasswordExpires, and __v from any
JSON response.
- Name field — add a reasonable
maxlength for basic input sanity.
⚠️ Note for maintainer
Adding select: false to password is a small breaking change — any existing
code that does User.findOne({ email }) and then compares user.password will
need .select('+password') added in that query. I'm happy to update the auth
controller as part of this PR if needed.
Can I take this issue?
I'd like to work on this — please assign it to me if it looks good. I'll raise
a PR with the updated schema.
Problem
The current
Userschema (models/User.js) works functionally but has a few gapsthat should be fixed before production use, mainly around validation and
sensitive data exposure.
Proposed Improvements
select: falsetopasswordfield so itdoesn't accidentally leak in API responses (e.g.
User.find()).select: falsetoresetPasswordTokenandresetPasswordExpiressince these are sensitive fields too.stay unique without breaking local users who don't have these fields.
toJSONtransform to automatically strippassword,resetPasswordToken,resetPasswordExpires, and__vfrom anyJSON response.
maxlengthfor basic input sanity.Adding
select: falsetopasswordis a small breaking change — any existingcode that does
User.findOne({ email })and then comparesuser.passwordwillneed
.select('+password')added in that query. I'm happy to update the authcontroller as part of this PR if needed.
Can I take this issue?
I'd like to work on this — please assign it to me if it looks good. I'll raise
a PR with the updated schema.