Manuel Coffin
Manuel Coffin
BABetter Auth
Created by Manuel Coffin on 3/15/2025 in #help
What's the difference between additionalFields and customSession?
Hi, it's a bit unclear to me the difference between additionalFields and customSession plugin. I am trying to migrate an app using Prisma, and I have a User schema that have many other fields. My understanding is that I should add those fields under user.additionalFields in auth.ts. However, most of those fields are many to many relationships with other tables, so I cannot use the proper type in additionalFields. For example, I have a table Role, and I want to be able to retrieve roles in my session, so should I do this?
user: {
additionalFields: {
roles: {
type: "", // <- What type should I use here?
},
}
}
user: {
additionalFields: {
roles: {
type: "", // <- What type should I use here?
},
}
}
I also a Preference table, and I want to have the user preferences in the session. Should I also add the preferences in the additionalFields? That way of extending the User schema seems a bit weird to me. Otherwise, I have tried to add the customSession plugin, like this:
customSession(async ({ user, session }) => {
const dbUser = await prisma.user.findUnique({
where: {
id: user.id,
},
include: {
roles: true,
preferences: true,
subscription: true,
},
});

return {
user: {
...user,
roles: dbUser?.roles.map((role) => role.name),
preferences: {
distanceUnit: dbUser?.preferences?.distanceUnit,
elevationUnit: dbUser?.preferences?.elevationUnit,
},
subscriptionStatus: dbUser?.subscription?.status,
},
session,
};
}),
customSession(async ({ user, session }) => {
const dbUser = await prisma.user.findUnique({
where: {
id: user.id,
},
include: {
roles: true,
preferences: true,
subscription: true,
},
});

return {
user: {
...user,
roles: dbUser?.roles.map((role) => role.name),
preferences: {
distanceUnit: dbUser?.preferences?.distanceUnit,
elevationUnit: dbUser?.preferences?.elevationUnit,
},
subscriptionStatus: dbUser?.subscription?.status,
},
session,
};
}),
Which is the way I would like to store those data in my session, the problem here is that the types are not inferred unless I also add additionalFields. I guess my questions would be: How can I simply add custom fields to my session that are also inferred? Could you explain the difference between additionalFields and customSession? Is there a way to have many-to-many relationships in additionalFields?
11 replies