Not able to get session type while using auth.api.getSession({headers: headers()})

I have an turborepo setup with below packages, i am trying to migrate from "next-auth" to "better-auth". I have all auth related code in seperate internal package "packages/auth". I facing very strange issue on which i am stuck from last 2 days. My problem is related to getting types for session, I am using auth.api.getSession to get session on serverside. I have tried everything, even when i am using this in auth.ts file itself it is not giving types. I have tried to use differnt tsconfig. Please help me. My packages:
{
"next": "14.2.1-canary.0",
"typescript": "5.2.2",
"better-auth": "^1.2.5",
"@trpc/server": "11.0.0-rc.332",
"drizzle-orm": "0.40.0",
"pg": "^8.13.0",
"@neondatabase/serverless": "^0.7.2",
}
{
"next": "14.2.1-canary.0",
"typescript": "5.2.2",
"better-auth": "^1.2.5",
"@trpc/server": "11.0.0-rc.332",
"drizzle-orm": "0.40.0",
"pg": "^8.13.0",
"@neondatabase/serverless": "^0.7.2",
}
usage: export const session = await auth.api.getSession({ header: headers() }); type of session object:
const session: {
[x: string]: any;
}
const session: {
[x: string]: any;
}
type of auth.api.getSession on hovering .getSession:
(property) getSession: <boolean>(context: {
headers: Headers;
query?: {
disableCookieCache?: boolean | undefined;
} | undefined;
asResponse?: boolean | undefined;
}) => Promise<{
[x: string]: any;
}> & {
...;
}
(property) getSession: <boolean>(context: {
headers: Headers;
query?: {
disableCookieCache?: boolean | undefined;
} | undefined;
asResponse?: boolean | undefined;
}) => Promise<{
[x: string]: any;
}> & {
...;
}
my tsconfig:
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Default",
"compilerOptions": {
"composite": false,
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"inlineSources": false,
"isolatedModules": true,
"moduleResolution": "node",
"noUnusedLocals": false,
"noUnusedParameters": false,
"preserveWatchOutput": true,
"skipLibCheck": true,
"strict": true,
"strictNullChecks": true
},
"exclude": ["node_modules"]
}
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Default",
"compilerOptions": {
"composite": false,
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"inlineSources": false,
"isolatedModules": true,
"moduleResolution": "node",
"noUnusedLocals": false,
"noUnusedParameters": false,
"preserveWatchOutput": true,
"skipLibCheck": true,
"strict": true,
"strictNullChecks": true
},
"exclude": ["node_modules"]
}
6 Replies
bekacru
bekacru4w ago
Hey could you also share your auth config?
Utkarsh Anand
Utkarsh AnandOP4w ago
My auth config is like below
export const auth = betterAuth({
// ======= Database config =======
database: drizzleAdapter(db, {
provider: "pg",
schema: {
...schema,
verification: schema.verificationToken
}
}),

// ========= Auth config =======
emailAndPassword: {
enabled: true
},
advanced: {
generateId: false
},

plugins: [magicLinkPlugin, nextCookies()]
});
export const auth = betterAuth({
// ======= Database config =======
database: drizzleAdapter(db, {
provider: "pg",
schema: {
...schema,
verification: schema.verificationToken
}
}),

// ========= Auth config =======
emailAndPassword: {
enabled: true
},
advanced: {
generateId: false
},

plugins: [magicLinkPlugin, nextCookies()]
});
But i am not sure if this is related to config, because when i tried it in some new cloned demo repo or other repo which are using better-auth, they are providing correct type of .getSession() even if i just pass an empty config like betterAuth({}). Also i have setup better-auth in an internal package of turborepo, which i used in webapp (nextjs app router) with transpilePackages: option in next.config.js. I don't know what i am missing, is it something in tsconfig or what? @bekacru
bekacru
bekacru4w ago
if you're trying to access auth in the nextjs app, it might has something to do wit how your building the local auth package. Check in the local auth package when you try to access auth.api.getSession if the types are working or not.
Utkarsh Anand
Utkarsh AnandOP4w ago
They are not working as i already described at first, and i am not building the internal package (auth) because it is directly used by nextjs using the transpile option. For example i have internal package which is not used in next app and i am creating the auth instance there using better-auth, then still i am not getting the types correctly. Can you share any repo link which is turborepo, nextjs@14 app router, trpc, drizzle with pg and better-auth?
bekacru
bekacru4w ago
GitHub
GitHub - Bekacru/create-t3-turbo: Clean and simple starter repo usi...
Clean and simple starter repo using the T3 Stack along with Expo React Native - Bekacru/create-t3-turbo
Utkarsh Anand
Utkarsh AnandOP3w ago
thanks @bekacru for sharing the repo, i fixed the problem, it was due to the typescirpt versoin "5.2.2" i am using, when i upgraded typescirpt verison to "5.6.3" it is giving me proper types.

Did you find this page helpful?