passport-local-mongoose
passport-local-mongoose copied to clipboard
Unique email (with unique username)
Is it possible to enforce unique email and username at the same time? All I see in the doc is "usernameUnique" option.
In my project that option works and I get an error when I try to register a duplicate username, however it's still possible to register a user with the same email.
As per my knowledge, no you can't do that.
But, what you can do is set that in you model like this,
email: {
type: String,
unique: true
}
*Extra info:
-
if "email" is not unique it will throw "duplicate key error"
-
if you want to handle that error specifically, use this
if (err.name === 'MongoError' && err.code === 11000) {
// handle error
}
That's what I ended up using. I don't know why it's not implemented, as it's not unusual to enforce both unique values.