issue getting session using mongodb

the login buttons work and i looking at the collection on mongodb it was able to insert the data, but when i try to check for a session on /dashboard using:
auth.api.getSession({
headers: await headers()
})
auth.api.getSession({
headers: await headers()
})
i get the following error:
ERROR INTERNAL_SERVER_ERROR Invalid id value Better Auth

at i (.next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:5035:23)
at .next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:5080:34
at Array.map (<anonymous>)
at Object.convertWhereClause (.next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:5075:23)
at Object.findOne (.next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:5183:66)
at Object.findSession (.next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:3721:29)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async .next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:1220:21
at async handle (.next\server\chunks\ssr\node_modules_0e8043._.js:1767:23)
at async Ie.a.<computed> [as getSession] (.next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:3395:17)
at async Promise.all (index 0)
at async Page (.next\server\chunks\ssr\[root of the server]__3677c3._.js:1370:21)

[APIError [BetterCallAPIError]: API Error: INTERNAL_SERVER_ERROR internal server error] {
status: 'INTERNAL_SERVER_ERROR',
headers: Headers {},
body: { message: 'internal server error', code: 'INTERNAL_SERVER_ERROR' },
[cause]: { message: 'internal server error', code: 'INTERNAL_SERVER_ERROR' }
}
GET /dashboard 500 in 1127ms
ERROR INTERNAL_SERVER_ERROR Invalid id value Better Auth

at i (.next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:5035:23)
at .next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:5080:34
at Array.map (<anonymous>)
at Object.convertWhereClause (.next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:5075:23)
at Object.findOne (.next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:5183:66)
at Object.findSession (.next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:3721:29)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async .next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:1220:21
at async handle (.next\server\chunks\ssr\node_modules_0e8043._.js:1767:23)
at async Ie.a.<computed> [as getSession] (.next\server\chunks\ssr\node_modules_better-auth_dist_b96472._.js:3395:17)
at async Promise.all (index 0)
at async Page (.next\server\chunks\ssr\[root of the server]__3677c3._.js:1370:21)

[APIError [BetterCallAPIError]: API Error: INTERNAL_SERVER_ERROR internal server error] {
status: 'INTERNAL_SERVER_ERROR',
headers: Headers {},
body: { message: 'internal server error', code: 'INTERNAL_SERVER_ERROR' },
[cause]: { message: 'internal server error', code: 'INTERNAL_SERVER_ERROR' }
}
GET /dashboard 500 in 1127ms
17 Replies
Ray
RayOP5mo ago
// auth.ts
import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import { db } from "@/lib/mongo";

export const auth = betterAuth({
database: mongodbAdapter(db),
socialProviders: {
twitter: {
clientId: process.env.TWITTER_CLIENT_ID!,
clientSecret: process.env.TWITTER_CLIENT_SECRET!,
},
linkedin: {
clientId: process.env.LINKEDIN_CLIENT_ID!,
clientSecret: process.env.LINKEDIN_CLIENT_SECRET!
}
}
});
// auth.ts
import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import { db } from "@/lib/mongo";

export const auth = betterAuth({
database: mongodbAdapter(db),
socialProviders: {
twitter: {
clientId: process.env.TWITTER_CLIENT_ID!,
clientSecret: process.env.TWITTER_CLIENT_SECRET!,
},
linkedin: {
clientId: process.env.LINKEDIN_CLIENT_ID!,
clientSecret: process.env.LINKEDIN_CLIENT_SECRET!
}
}
});
// mongo.ts
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = process.env.MONGO_URI!;

const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});

export const db = client.db("users");
// mongo.ts
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = process.env.MONGO_URI!;

const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});

export const db = client.db("users");
tried turning on advanced.generateId to false
bekacru
bekacru5mo ago
could you share your schema and your auth config
Ray
RayOP5mo ago
i dont have a custom schema
// auth.ts
import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import { db } from "@/lib/mongo";

export const auth = betterAuth({
database: mongodbAdapter(db),
socialProviders: {
twitter: {
clientId: process.env.TWITTER_CLIENT_ID!,
clientSecret: process.env.TWITTER_CLIENT_SECRET!,
},
linkedin: {
clientId: process.env.LINKEDIN_CLIENT_ID!,
clientSecret: process.env.LINKEDIN_CLIENT_SECRET!
}
},
user: {
deleteUser: {
enabled: true
}
},
advanced: {
generateId: false,
}
});
// auth.ts
import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import { db } from "@/lib/mongo";

export const auth = betterAuth({
database: mongodbAdapter(db),
socialProviders: {
twitter: {
clientId: process.env.TWITTER_CLIENT_ID!,
clientSecret: process.env.TWITTER_CLIENT_SECRET!,
},
linkedin: {
clientId: process.env.LINKEDIN_CLIENT_ID!,
clientSecret: process.env.LINKEDIN_CLIENT_SECRET!
}
},
user: {
deleteUser: {
enabled: true
}
},
advanced: {
generateId: false,
}
});
bekacru
bekacru5mo ago
if you're using mongodb adapter don't disable generateID
Ray
RayOP5mo ago
i get the error both with or without the generateid anything else i could try?
bekacru
bekacru5mo ago
by any chance do you already have data in your db?
Ray
RayOP5mo ago
i mean the login buttons work and adds the information to the db but then when it does the call back to /dashboard i get the internal server error and any future reloads with any data in the db and session cookie
bekacru
bekacru5mo ago
so getSession is failing?
Ray
RayOP5mo ago
yes
bekacru
bekacru5mo ago
could you try to reset the db and check if it works?
Ray
RayOP5mo ago
new db? yeah sure
bekacru
bekacru5mo ago
yeah. invlaid id is thrown from better auth when the id couldnt be converted to string or back to ObjectId
Ray
RayOP5mo ago
tried on a new db and same issue
ERROR INTERNAL_SERVER_ERROR Invalid id value Better Auth


ERROR INTERNAL_SERVER_ERROR API Error: INTERNAL_SERVER_ERROR Failed to get session Better Auth



GET /api/auth/get-session 500 in 677ms
ERROR INTERNAL_SERVER_ERROR Invalid id value Better Auth


ERROR INTERNAL_SERVER_ERROR API Error: INTERNAL_SERVER_ERROR Failed to get session Better Auth



GET /api/auth/get-session 500 in 677ms
No description
Ray
RayOP5mo ago
data gets added into db though
No description
Ray
RayOP5mo ago
wtf i fixed it? okay so idk why but turning off --turbo on nextjs worked
SePeHr
SePeHr3w ago
I lost a week of my life until the problem was solved :pepe_cry: You saved my life :peepoLove:
Ray
RayOP3w ago
😭😭 I feel you ❤️

Did you find this page helpful?