passport-local-mongoose icon indicating copy to clipboard operation
passport-local-mongoose copied to clipboard

Unique email (with unique username)

Open cd4d opened this issue 3 years ago • 2 comments

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.

cd4d avatar Mar 27 '21 22:03 cd4d

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
 }

rajan-31 avatar Apr 11 '21 18:04 rajan-31

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.

cd4d avatar Apr 11 '21 19:04 cd4d