Addition field not returned
I added a role field to the user schema but for some reason, it's not getting returned after calling the sign-in api endpoint. Here is my auth.ts. I know that I'm not supposed to add the role to the session conventionally but I did it so that I don't hit the database for role checks. I need the user sign-in endpoint to include the role field.
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
}),
emailAndPassword: {
enabled: true,
},
user: {
additionalFields: {
role: {
type: "string",
required: true,
defaultValue: "customer",
input: true,
},
},
},
session: {
additionalFields: {
role: {
type: "string",
required: true,
input: true,
},
},
},
databaseHooks: {
session: {
create: {
before: async (session) => {
// Query the user's role from the database using Drizzle
const user = await db.query.user.findFirst({
where: (users, { eq }) => eq(users.id, session.userId),
columns: {
role: true,
},
});
return {
data: {
...session,
role: user?.role || "customer",
},
};
},
},
},
},
secondaryStorage: {
get: async (key) => {
const value = await redis.get(key);
return value ?? null;
},
set: async (key, value, ttl) => {
if (ttl) {
await redis.set(key, value, "EX", ttl);
} else {
await redis.set(key, value);
}
},
delete: async (key) => {
await redis.del(key);
},
},
advanced: {
cookies: {
session_token: {
name: "sessionId",
},
},
},
});
6 Replies
Try adding a
returned
field to the role and making it trueJust tried that, did not work
Did you solve it?
We don't allow for customizing how sign-in or sign-up endpoint returns data as of now.
We need it. What can we do in this case?
Make an additional fetch.