how to return more data from `/get-session`

hello, I would want to store user's balance inside session object, instead of fetching it manually I tried to create a plugin for better auth, but it doesn't work (it worked a few days before, I didn't change code nor updated dependencies...)
import type { BetterAuthClientPlugin } from "better-auth/client";
import type { BetterAuthPlugin } from "better-auth/plugins";

export const balancePlugin = () => {
return {
id: "balance",
schema: {
user: {
fields: {
balance: {
type: "number",
required: false,
sortable: false,
unique: false,
returned: true,
},
},
},
},
} satisfies BetterAuthPlugin;
};

export const balanceClientPlugin = () => {
return {
id: "balance",
$InferServerPlugin: {} as ReturnType<typeof balancePlugin>,
} satisfies BetterAuthClientPlugin;
};
import type { BetterAuthClientPlugin } from "better-auth/client";
import type { BetterAuthPlugin } from "better-auth/plugins";

export const balancePlugin = () => {
return {
id: "balance",
schema: {
user: {
fields: {
balance: {
type: "number",
required: false,
sortable: false,
unique: false,
returned: true,
},
},
},
},
} satisfies BetterAuthPlugin;
};

export const balanceClientPlugin = () => {
return {
id: "balance",
$InferServerPlugin: {} as ReturnType<typeof balancePlugin>,
} satisfies BetterAuthClientPlugin;
};
so my question is, how to store more data in the session object?
2 Replies
Glen Kurio
Glen Kurio3w ago
If I'm not mistaken you don't need to create a custom plugin, all you need to do is to extend session with CustomSession built-in plugin:
import { customSession } from "better-auth/plugins";

export const auth = betterAuth({
plugins: [
customSession(async ({ user, session }) => {
const roles = findUserRoles(session.session.userId);
return {
roles,
user: {
...user,
newField: "newField",
},
session
};
}),
],
});
import { customSession } from "better-auth/plugins";

export const auth = betterAuth({
plugins: [
customSession(async ({ user, session }) => {
const roles = findUserRoles(session.session.userId);
return {
roles,
user: {
...user,
newField: "newField",
},
session
};
}),
],
});
Docs link to this: https://www.better-auth.com/docs/concepts/session-management#customizing-session-response
Session Management | Better Auth
Better Auth session management.
rhitune
rhitune3w ago
did you tried inferring fields?

Did you find this page helpful?