BA
Better Authโ€ข3d ago
snouzy

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",
},
},
},
},
});
3 Replies
Ping
Pingโ€ข2d ago
I dont think we support a type array for field type, but we do have string[] as well as number[]
snouzy
snouzyOPโ€ขthis hour
Oups, that was for testing purposes. I just missed the fact that i needed customSessionClient instead of inferAdditionalFields Now everything is fully typed, even the relation with pages
export const authClient = createAuthClient({
/** The base URL of the server (optional if you're using the same domain) */
baseURL: getServerUrl(),
plugins: [customSessionClient<typeof auth>()],
});
export const authClient = createAuthClient({
/** The base URL of the server (optional if you're using the same domain) */
baseURL: getServerUrl(),
plugins: [customSessionClient<typeof auth>()],
});
What's your opinion about this ? โœ… 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
Ping
Pingโ€ข22h ago
Depends, I have no clue what the purpose of pages is, how large of an array it may be, etc etc. If you think pages is heavy, or could take a little bit to fetch, then maybe it's not worth it? not sure.

Did you find this page helpful?