Passport authentication in js

30 views Asked by At
MongoServerError: E11000 duplicate key error collection: usersDB.users index: username_1 dup key: { username: null }

at E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\operations\insert.js:53:33

at E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\cmap\connection_pool.js:308:25

at E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\sdam\server.js:213:17

at handleOperationResult (E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\sdam\server.js:329:20)

at Connection.onMessage (E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\cmap\connection.js:219:9)

at MessageStream. (E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\cmap\connection.js:60:60)

at MessageStream.emit (node:events:527:28)

at processIncomingData (E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\cmap\message_stream.js:132:20)

at MessageStream._write (E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\cmap\message_stream.js:33:9)

at writeOrBuffer (node:internal/streams/writable:389:12) {

index: 0,

code: 11000,

keyPattern: { username: 1 },

keyValue: { username: null },

[Symbol(errorLabels)]: Set(0) {}

}

I wasn't able to register through facebook once after i registered and logout out from google auth. How to solve this issue?

1

There are 1 answers

0
Dhaval Italiya On

You are facing this error not because of Passport authentication, but because you made the username field unique and you are trying to register a new user with an existing username.

While authenticating you may be registering a user, in that case, you have to check if the username already exists or not, if not exist go ahead and register your user, otherwise, show the proper message or tell them to choose another username since same one exists in your table/modal.

The flow should be, to check for the users in dB, if not exist register them if exist login them.