DatabaseHooks Session Create Before

databaseHooks: {
session: {
create: {
before: async (session) => {
const activeOrganization = await getActiveOrganization(
session.userId,
);
const activeOrganizationSlug = await getOrganizationSlug(
activeOrganization?.id,
);

return {
data: {
...session,
activeOrganization: {
id: activeOrganization?.id,
slug: activeOrganizationSlug?.slug,
},
},
};
},
},
},
},
databaseHooks: {
session: {
create: {
before: async (session) => {
const activeOrganization = await getActiveOrganization(
session.userId,
);
const activeOrganizationSlug = await getOrganizationSlug(
activeOrganization?.id,
);

return {
data: {
...session,
activeOrganization: {
id: activeOrganization?.id,
slug: activeOrganizationSlug?.slug,
},
},
};
},
},
},
},
Why won't authClient.getSession() show my new activeOrganization property with id an slug? I only see activeOrganizationId which I want to get rid of now that I am adding slug. I am pretty sure activeOrganizationIdis added automatically by the organization plugin
2 Replies
Ping
Ping2mo ago
We don't support just passing an object in the data as it means you're inserting an object into your DB - which we don't support. You'll need to change that to be something like activeOrganizationId and activeOrganizationSlug. Also make sure to add these as additional fields part of the session: https://www.better-auth.com/docs/concepts/typescript#additional-fields
TypeScript | Better Auth
Better Auth TypeScript integration.
Acro
AcroOP4w ago
What I did, 1. Update database hook as you see 2. Update betterAuth server instance session data 3. Add column in session table with drizzle 4. Update authClient with new attribute (separate setup) weird activeOrganizationSlug is null. But now that I think about it, I shouldn’t be adding properties of another entity in sessions like this… I think the better way is authClient maybe supported getting relationships if id is present. If not, need to make two network calls. 1. Getting session. 2. Get active organization id from session and use that to make a network call to getFullOrganization.

Did you find this page helpful?