useSession query behaviour?

I have a couple of extra fields on my user table that I like to have when I call useSession, but currently it seems to only include the base "id", "name", "email", "emailVerified", "image" however when I turn the logger on in drizzle it seems like when get-session endpoint get's called it actually queries all the data from the table. I guess my question is, since the data is already queried how can i pass it so it's available in my useSession hook.
No description
14 Replies
deved9036
deved9036OP5mo ago
also looked into customSession but you end up doing another query
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "~/db";
import { verifications, accounts, sessions, users } from "~/db/schema";

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: {
verification: verifications,
account: accounts,
session: sessions,
user: users,
},
}),
//Also if I do a db query inside a custom session you end up querying it twice which is no bueno
//somethin like this
plugins: [customSession(async{user,session} => {
const dbUser = await db.query.users.findFirst()
..
...
//return the extra fields from db here
return {session, user: {...user, points: dbUser.points,}} //etc etc
})]

secret: process.env.BETTER_AUTH_SECRET,
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!,
},
},
});
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "~/db";
import { verifications, accounts, sessions, users } from "~/db/schema";

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: {
verification: verifications,
account: accounts,
session: sessions,
user: users,
},
}),
//Also if I do a db query inside a custom session you end up querying it twice which is no bueno
//somethin like this
plugins: [customSession(async{user,session} => {
const dbUser = await db.query.users.findFirst()
..
...
//return the extra fields from db here
return {session, user: {...user, points: dbUser.points,}} //etc etc
})]

secret: process.env.BETTER_AUTH_SECRET,
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!,
},
},
});
Soheel
Soheel5mo ago
https://www.better-auth.com/docs/reference/options Have you tried the additionalFields in the betterAuth config ?
import { betterAuth } from "better-auth";
export const auth = betterAuth({
user: {
modelName: "users",
fields: {
email: "emailAddress",
name: "fullName"
},
additionalFields: {
customField: {
type: "string",
nullable: true
}
},
changeEmail: {
enabled: true,
sendChangeEmailVerification: async ({ user, newEmail, url, token }) => {
// Send change email verification
}
},
deleteUser: {
enabled: true,
sendDeleteAccountVerification: async ({ user, url, token }) => {
// Send delete account verification
},
beforeDelete: async (user) => {
// Perform actions before user deletion
},
afterDelete: async (user) => {
// Perform cleanup after user deletion
}
}
},
})
import { betterAuth } from "better-auth";
export const auth = betterAuth({
user: {
modelName: "users",
fields: {
email: "emailAddress",
name: "fullName"
},
additionalFields: {
customField: {
type: "string",
nullable: true
}
},
changeEmail: {
enabled: true,
sendChangeEmailVerification: async ({ user, newEmail, url, token }) => {
// Send change email verification
}
},
deleteUser: {
enabled: true,
sendDeleteAccountVerification: async ({ user, url, token }) => {
// Send delete account verification
},
beforeDelete: async (user) => {
// Perform actions before user deletion
},
afterDelete: async (user) => {
// Perform cleanup after user deletion
}
}
},
})
Options | Better Auth
Better Auth configuration options reference.
Soheel
Soheel5mo ago
I use them myself, but didn't test whether useSession returns them, I didn't need them for that sake
deved9036
deved9036OP5mo ago
just gave the additionalFields a go, thanks for sharing but yeah it doesnt seem to return any extra fields
Soheel
Soheel5mo ago
sadge
deved9036
deved9036OP5mo ago
actually nevermind! it did end up woking belss bless
Soheel
Soheel5mo ago
Love to hear it :D
deved9036
deved9036OP5mo ago
for anyone wondering
Soheel
Soheel5mo ago
This library is lowkey really awesome, but sometimes you really need to dig through the docs at points you would not expect initially :D
deved9036
deved9036OP5mo ago
make sure there is no missmatch between your schema name and fieldName
deved9036
deved9036OP5mo ago
No description
deved9036
deved9036OP5mo ago
this works yeah I've been looking through the source code a lot in the past 2 days or so, lots of goodies built into it
Soheel
Soheel5mo ago
Btw you can set this as "Solved" so people can more easily find it
deved9036
deved9036OP5mo ago
oh my apologies!

Did you find this page helpful?