snouzy
snouzy
BABetter Auth
Created by snouzy on 4/18/2025 in #help
Is it a possible to use a relation table as type for fields in additionalFields ? (beginner ๐Ÿ˜…)
I'm currently working with better-auth and Iโ€™d like to access the pages field of the user directly from the session (code snippet below). (It's another table). Is it possible / a good idea to include this kind of data directly in the session (via customSession) or should I rather fetch it separately using a /me endpoint after login? Iโ€™m not an auth expert, so Iโ€™m not 100% sure what the best practice is here โ€“ any feedback or guidance would be super appreciated! ๐Ÿ™ Hereโ€™s what Iโ€™ve got so far ๐Ÿ‘‡ TLDR ; โœ… Is it okay to include pages directly in the session? ๐Ÿค” Or is it better to keep the session lightweight and fetch /me on the client side?
export const auth = betterAuth({
plugins: [
customSession(async ({ user, session }) => {
const userFromDB = await prisma.user.findUnique({
where: {
id: user.id,
},
select: {
...,
pages: {
select: {
id: true,
template: true,
slug: true,
},
},
},
});

return {
user: userFromDB,
session,
};
}),
nextCookies(),
],
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
session: {
cookieCache: {
enabled: true,
maxAge: 60 * 60 * 24 * 30, // 30 days
},
},
user: {
fields: {

},
additionalFields: {
firstName: {
type: "string",
required: true,
},
lastName: {
type: "string",
required: true,
},
pages: {
type: "array",
required: true,
items: {
type: "string",
},
},
},
},
});
export const auth = betterAuth({
plugins: [
customSession(async ({ user, session }) => {
const userFromDB = await prisma.user.findUnique({
where: {
id: user.id,
},
select: {
...,
pages: {
select: {
id: true,
template: true,
slug: true,
},
},
},
});

return {
user: userFromDB,
session,
};
}),
nextCookies(),
],
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
session: {
cookieCache: {
enabled: true,
maxAge: 60 * 60 * 24 * 30, // 30 days
},
},
user: {
fields: {

},
additionalFields: {
firstName: {
type: "string",
required: true,
},
lastName: {
type: "string",
required: true,
},
pages: {
type: "array",
required: true,
items: {
type: "string",
},
},
},
},
});
4 replies