BA
Better Auth•3w ago
Kasszz

Adding custom properties to Organization members

Hey, The title says it all 😛 I would like to add custom properties to the members of an organization but I don't see any docs on it. I tried adding it to the table and hope the auth.api.getActiveMember() will pick it up but no luck 😦 Is this possible, if so, how? Thanks 🙂
6 Replies
Megamind
Megamind•2w ago
Hi @Kasszz any update on this? i am also trying to add custom/additional fields to members. i am using organizations plugin. could someone please guide me on this? i have defined the schema in Prisma manually, but the values are not being added here is my code.
await auth.api.addMember({
body: { organizationId,
userId: user.id, role:faker.helpers.arrayElement(["admin", "member"] as const),
customFields: {
address: faker.location.streetAddress(),
zipCode: faker.location.zipCode(),
},
},
});

await auth.api.addMember({
body: { organizationId,
userId: user.id, role:faker.helpers.arrayElement(["admin", "member"] as const),
customFields: {
address: faker.location.streetAddress(),
zipCode: faker.location.zipCode(),
},
},
});

KiNFiSH
KiNFiSH•2w ago
can you show me how you are passing the additional fields
Megamind
Megamind•2w ago
The docs has examples of adding new fields to the user schema. But it doesn't give suggestions for adding new fields to organization members schema So i modified the schema in Prisma directly
model Member {
id String @id @map("_id")
organizationId String
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
role String
customFields Json? // Stores the custom field values as JSON
// customFields [
// {
// "address": "123 Main St",
// "zipCode": "12345"
// }
// ]
createdAt DateTime

@@map("member")
}
model Member {
id String @id @map("_id")
organizationId String
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
role String
customFields Json? // Stores the custom field values as JSON
// customFields [
// {
// "address": "123 Main St",
// "zipCode": "12345"
// }
// ]
createdAt DateTime

@@map("member")
}
Kasszz
KasszzOP•2w ago
Hey, not gotten any further yet ( busy at work ^^ ). But yea like @Megamind said, you can add them to your Prisma or in my case Drizzle schemas. But better-auth then has no idea about it and doesn't retrieve it on getActiveMember()
KiNFiSH
KiNFiSH•2w ago
It is extendible I mean the additionalField can also work with organization table it is not limited for a specific table. you should tweak some stuff manually for that tho First add it there and try to infer the type by using the link above and try to mutate data based on that .
Kasszz
KasszzOP•6d ago
Hey, I'm trying to get it to work but when adding a "test" column on the members table that is non NULL. The code breaks when I'm trying to make an organisation with:
const organization = await auth.api.createOrganization({
headers: nextHeaders,
body: {
name: data.name,
slug,
},
})
const organization = await auth.api.createOrganization({
headers: nextHeaders,
body: {
name: data.name,
slug,
},
})
Is there an example that I could use because I feel like I'm just being a noob 😛

Did you find this page helpful?