Adding custom user fields (role, address, etc.) to auth.api.getSession()

When I call:
const session = await auth.api.getSession({
headers: await headers(),
});
const session = await auth.api.getSession({
headers: await headers(),
});
I get the following Drizzle Query:
Query: select "id", "name", "email", "email_verified", "image", "role", "created_at", "updated_at" from "better_auth"."user" "usersTable" where "usersTable"."id" = $1 limit $2 -- params: ["hKzWaLXtHajuOzaMY8iaOW4WakQ9vWJQ", 1]
Query: select "id", "name", "email", "email_verified", "image", "role", "created_at", "updated_at" from "better_auth"."user" "usersTable" where "usersTable"."id" = $1 limit $2 -- params: ["hKzWaLXtHajuOzaMY8iaOW4WakQ9vWJQ", 1]
Is there a way I can customize this query (in my settings or in the auth.api.getSession call) to include values like the role, address, subscription, etc, so that I'm able to access it from session.user object? I've seen the docs on customizing the session but if I use the suggested implementation I'll be hitting the database twice - once to get the default fields, and the second time to get the custom fields - and I don't want that. Is there a way I can directly customize the query called by auth.api.getSession so we're querying the database only once to get all of the information required? Thanks!
Session Management | Better Auth
Better Auth session management.
1 Reply
bekacru
bekacru2mo ago
you can potentially override it using before hook, but we don't recommend that and currently you'd be required to make an additional query.

Did you find this page helpful?