Malformed ObjectID in prisma mongoDb #511

Closed
opened 2026-03-13 07:50:27 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @nicolofranceschi on GitHub (Jan 2, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. git clone https://github.com/Bekacru/t3-app-better-auth.git

  2. install and update prima to mongodb

database: prismaAdapter(prisma, {
provider: "mongodb",
}),

and schema.prisma with the client npx @better-auth/cli generate

on auth

Invalid u[s(t)].create() invocation in
.next/server/chunks/69d23_better-auth_dist_ee342e._.js:5028:100

5025 return {
5026 id: "prisma",
5027 async create (o) {
→ 5028 let { model: t, data: a, select: r } = o, y = c(a, t, "create"), g = await u[s(t)].create(
Inconsistent column data: Malformed ObjectID: provided hex string representation must be exactly 12 bytes, instead got: "61qnMnpyuwrSdikMGvKYU", length 21 for the field 'id'.

Current vs. Expected behavior

the id is wrong for mongodb

What version of Better Auth are you using?

1.1.7

Provide environment information

- mac os - m2

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { nextCookies } from "better-auth/next-js";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { PrismaClient } from "@prisma/client";
import { cache } from "react";
import { headers } from "next/headers";

const prisma = new PrismaClient();

export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: "mongodb",
  }),
  emailAndPassword: {
    enabled: true,
    async sendResetPassword({ url }) {
      console.log("Reset password URL:", url);
    },
  },
  plugins: [
    nextCookies()
  ],
  socialProviders: {
    github: {
      clientId: process.env.GITHUB_CLIENT_ID!,
      clientSecret: process.env.GITHUB_CLIENT_SECRET!,
    },
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID!,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
    },
  },
});

export const getSession = cache(async () => {
  return await auth.api.getSession({
    headers: await headers()
  })
})

Additional context

no

Originally created by @nicolofranceschi on GitHub (Jan 2, 2025). ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce 1. git clone https://github.com/Bekacru/t3-app-better-auth.git 2. install and update prima to mongodb database: prismaAdapter(prisma, { provider: "mongodb", }), and schema.prisma with the client npx @better-auth/cli generate on auth Invalid u[s(t)].create() invocation in .next/server/chunks/69d23_better-auth_dist_ee342e._.js:5028:100 5025 return { 5026 id: "prisma", 5027 async create (o) { → 5028 let { model: t, data: a, select: r } = o, y = c(a, t, "create"), g = await u[s(t)].create( Inconsistent column data: Malformed ObjectID: provided hex string representation must be exactly 12 bytes, instead got: "61qnMnpyuwrSdikMGvKYU", length 21 for the field 'id'. ### Current vs. Expected behavior the id is wrong for mongodb ### What version of Better Auth are you using? 1.1.7 ### Provide environment information ```bash - mac os - m2 ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { nextCookies } from "better-auth/next-js"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { PrismaClient } from "@prisma/client"; import { cache } from "react"; import { headers } from "next/headers"; const prisma = new PrismaClient(); export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "mongodb", }), emailAndPassword: { enabled: true, async sendResetPassword({ url }) { console.log("Reset password URL:", url); }, }, plugins: [ nextCookies() ], socialProviders: { github: { clientId: process.env.GITHUB_CLIENT_ID!, clientSecret: process.env.GITHUB_CLIENT_SECRET!, }, google: { clientId: process.env.GOOGLE_CLIENT_ID!, clientSecret: process.env.GOOGLE_CLIENT_SECRET!, }, }, }); export const getSession = cache(async () => { return await auth.api.getSession({ headers: await headers() }) }) ``` ### Additional context no
GiteaMirror added the bug label 2026-03-13 07:50:28 -05:00
Author
Owner

@Bekacru commented on GitHub (Jan 2, 2025):

make sure to disbale auto generated IDs by setting advanced.generateId to false

@Bekacru commented on GitHub (Jan 2, 2025): make sure to disbale auto generated IDs by setting `advanced.generateId` to `false`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#511