Missing field in session object with drizzle adapter

I'm using BetterAuth with the drizzle adapter in my next.js application and have added a custom isAdmin field to my user schema. Despite the field existing in my database and configuring additionalFields in BetterAuth, session.user does not include isAdmin when I log it. I've also attempted to use the inferAdditionalFields plugin on the client side, but it still doesn't appear in the session object. I'm looking for a solution to correctly include isAdmin in the session data. here's my code for auth:
export const auth = betterAuth({
secret: process.env.SECRET!,
database: drizzleAdapter(db, {
provider: "pg",
schema: {
user,
verification,
session,
account,
},
}),
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
},
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID! as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET! as string,
},
},
plugins: [username()],
})
export const auth = betterAuth({
secret: process.env.SECRET!,
database: drizzleAdapter(db, {
provider: "pg",
schema: {
user,
verification,
session,
account,
},
}),
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
},
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID! as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET! as string,
},
},
plugins: [username()],
})
and here's my code for authClient:
export const authClient = createAuthClient({
plugins: [
usernameClient(),
inferAdditionalFields({
user: {
isAdmin: {
type: "boolean",
},
},
}),
],
baseURL: "http://localhost:3000",
});
export type Session = typeof authClient.$Infer.Session;
export const { signIn, signOut, signUp, useSession } = authClient;
export const authClient = createAuthClient({
plugins: [
usernameClient(),
inferAdditionalFields({
user: {
isAdmin: {
type: "boolean",
},
},
}),
],
baseURL: "http://localhost:3000",
});
export type Session = typeof authClient.$Infer.Session;
export const { signIn, signOut, signUp, useSession } = authClient;
No description
No description
Solution:
Don't put the additional fields in the schema. It should be at the betterAuth level. Here is how it should be: ```ts...
Jump to solution
6 Replies
Ping
Ping7d ago
Make sure to actually add the additional fields to your server auth config as well. EG:
import { betterAuth } from "better-auth";

export const auth = betterAuth({
user: {
additionalFields: {
role: {
type: "string",
required: false,
defaultValue: "user",
input: false // don't allow user to set role
},
lang: {
type: "string",
required: false,
defaultValue: "en",
}
}
}
})
import { betterAuth } from "better-auth";

export const auth = betterAuth({
user: {
additionalFields: {
role: {
type: "string",
required: false,
defaultValue: "user",
input: false // don't allow user to set role
},
lang: {
type: "string",
required: false,
defaultValue: "en",
}
}
}
})
More info here Most cases, you define the additional fields here, then on the auth client, when using inferAdditionalFields, the generic would be typeof auth. Like this:
import { inferAdditionalFields } from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
import type { auth } from "./auth";

export const authClient = createAuthClient({
plugins: [inferAdditionalFields<typeof auth>()],
});
import { inferAdditionalFields } from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
import type { auth } from "./auth";

export const authClient = createAuthClient({
plugins: [inferAdditionalFields<typeof auth>()],
});
Sthap
SthapOP7d ago
I tried the solution you provided, but instead, I encountered an internal server error
No description
Sthap
SthapOP7d ago
my auth client code:
export const authClient = createAuthClient({
plugins: [
usernameClient(),
inferAdditionalFields<typeof auth>(),
],
baseURL: "http://localhost:3000",
})
export type Session = typeof authClient.$Infer.Session;
export const { signIn, signOut, signUp, useSession } = authClient;
export const authClient = createAuthClient({
plugins: [
usernameClient(),
inferAdditionalFields<typeof auth>(),
],
baseURL: "http://localhost:3000",
})
export type Session = typeof authClient.$Infer.Session;
export const { signIn, signOut, signUp, useSession } = authClient;
my server auth config:
export const auth = betterAuth({
secret: process.env.SECRET!,
database: drizzleAdapter(db, {
provider: "pg",
schema: {
additionalFields: {
isAdmin: {
type: "boolean",
required: true,
defaultValue: false,
input: false
},
},
verification,
session,
account,
},
}),
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
},
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID! as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET! as string,
},
},
plugins: [username()],
})
export const auth = betterAuth({
secret: process.env.SECRET!,
database: drizzleAdapter(db, {
provider: "pg",
schema: {
additionalFields: {
isAdmin: {
type: "boolean",
required: true,
defaultValue: false,
input: false
},
},
verification,
session,
account,
},
}),
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
},
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID! as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET! as string,
},
},
plugins: [username()],
})
The error code state {"message":"Failed to get session","code":"FAILED_TO_GET_SESSION"} from /get-session
Solution
Ping
Ping7d ago
Don't put the additional fields in the schema. It should be at the betterAuth level. Here is how it should be:
export const auth = betterAuth({
secret: process.env.SECRET!,
database: drizzleAdapter(db, {
provider: "pg",
schema: {
user,
verification,
session,
account,
},
}),
user:{
additionalFields: {
isAdmin: {
type: "boolean",
required: true,
defaultValue: false,
input: false
},
},
},
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
},
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID! as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET! as string,
},
},
plugins: [username()],
})
export const auth = betterAuth({
secret: process.env.SECRET!,
database: drizzleAdapter(db, {
provider: "pg",
schema: {
user,
verification,
session,
account,
},
}),
user:{
additionalFields: {
isAdmin: {
type: "boolean",
required: true,
defaultValue: false,
input: false
},
},
},
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
},
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID! as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET! as string,
},
},
plugins: [username()],
})
Sthap
SthapOP7d ago
Oh yes you are right! It's my bad ;(( I really appreciate it , thanks a lot!
Ping
Ping7d ago
No worries :)

Did you find this page helpful?