0

Here is the error that I get whenever someone logs in. I don't know why this is happening honestly.

2022-08-08T17:54:41.752185+00:00 app[web.1]:   message: 'E11000 duplicate key error collection: cored-js-6.users index: userId_1 dup key: { userId: null }',
2022-08-08T17:54:41.752186+00:00 app[web.1]:   stack: 'MongoServerError: E11000 duplicate key error collection: cored-js-6.users index: userId_1 dup key: { userId: null }\n' +
2022-08-08T17:54:41.752187+00:00 app[web.1]:     '    at /app/node_modules/mongodb/lib/operations/insert.js:53:33\n' +
2022-08-08T17:54:41.752187+00:00 app[web.1]:     '    at /app/node_modules/mongodb/lib/cmap/connection_pool.js:299:25\n' +
2022-08-08T17:54:41.752188+00:00 app[web.1]:     '    at /app/node_modules/mongodb/lib/sdam/server.js:212:17\n' +
2022-08-08T17:54:41.752188+00:00 app[web.1]:     '    at handleOperationResult (/app/node_modules/mongodb/lib/sdam/server.js:287:20)\n' +
2022-08-08T17:54:41.752189+00:00 app[web.1]:     '    at Connection.onMessage (/app/node_modules/mongodb/lib/cmap/connection.js:219:9)\n' +
2022-08-08T17:54:41.752189+00:00 app[web.1]:     '    at MessageStream.<anonymous> (/app/node_modules/mongodb/lib/cmap/connection.js:60:60)\n' +
2022-08-08T17:54:41.752190+00:00 app[web.1]:     '    at MessageStream.emit (node:events:527:28)\n' +
2022-08-08T17:54:41.752190+00:00 app[web.1]:     '    at processIncomingData (/app/node_modules/mongodb/lib/cmap/message_stream.js:132:20)\n' +
2022-08-08T17:54:41.752190+00:00 app[web.1]:     '    at MessageStream._write (/app/node_modules/mongodb/lib/cmap/message_stream.js:33:9)\n' +
2022-08-08T17:54:41.752191+00:00 app[web.1]:     '    at writeOrBuffer (node:internal/streams/writable:389:12)',
2022-08-08T17:54:41.752192+00:00 app[web.1]:   name: 'MongoServerError'
2022-08-08T17:54:41.752192+00:00 app[web.1]: }

Code of [...nextauth].ts:

let client: any;
if (Configuration.Miscellaneous.Development) {
  client = new MongoClient(Configuration.Development.Mongo);
} else {
  client = new MongoClient(Configuration.Production.Mongo);
}
const clientPromise = client.connect();
export const authOptions: NextAuthOptions = {
  adapter: MongoDBAdapter(clientPromise),
  providers: [
    GoogleProvider({
      clientId: Configuration.Google.ClientID,
      clientSecret: Configuration.Google.ClientSecret,
      authorization: {
        params: {
          prompt: "consent",
          access_type: "offline",
          response_type: "code",
        },
      },
    }),
  ],
  jwt: {
    maxAge: 60 * 60 * 24 * 7,
  },
  callbacks: {
    session: async ({ session, token }) => {
      if (session?.user) {
        (session.user as any).id = token.uid;
      }
      return session;
    },
    jwt: async ({ user, token }) => {
      if (user) {
        token.uid = user.id;
      }
      return Promise.resolve(token);
    },
  },
  session: {
    strategy: "jwt",
  },
};

export default NextAuth(authOptions);

I've reset the model and deleted the database with no luck yet. I've even tried using providers other than google, but with the same result. Why is it showing the id as null?

FSCYT
  • 104
  • 7

1 Answers1

0

As explained here, you get this error because there's already a record with null and your field is a unique index.

You can check more about unique index here