BA
Better Auth•3mo ago
Kazz

Updating organization metadata

Currently, after updating organization metadata, the metadata gets stringified twice, causing the data to be stored as '[object object]' This should solve the problem theoretically by checking if the metadata already a string or not and stringifiying it based on that. I didn't have time to create a PR and clone the project so i just drafted this quickly from what I saw in the file File path: packages/better-auth/src/plugins/organization/adapter.ts Untested Code:
updateOrganization: async (
organizationId: string,
data: Partial<Organization>,
) => {
let organizationUpdate = { ...data };
if (data.metadata) {
organizationUpdate.metadata = typeof data.metadata === 'string'
? data.metadata
: JSON.stringify(data.metadata);
}

const organization = await adapter.update<Organization>({
model: "organization",
where: [
{
field: "id",
value: organizationId,
},
],
update: organizationUpdate,
});
if (!organization) {
return null;
}
return {
...organization,
metadata: organization.metadata
? JSON.parse(organization.metadata)
: undefined,
};
},
updateOrganization: async (
organizationId: string,
data: Partial<Organization>,
) => {
let organizationUpdate = { ...data };
if (data.metadata) {
organizationUpdate.metadata = typeof data.metadata === 'string'
? data.metadata
: JSON.stringify(data.metadata);
}

const organization = await adapter.update<Organization>({
model: "organization",
where: [
{
field: "id",
value: organizationId,
},
],
update: organizationUpdate,
});
if (!organization) {
return null;
}
return {
...organization,
metadata: organization.metadata
? JSON.parse(organization.metadata)
: undefined,
};
},
3 Replies
Kazz
KazzOP•3mo ago
@bekacru Sorry didn't have time to create a PR, here is what I think could solve this https://github.com/better-auth/better-auth/issues/698#issuecomment-2508770820
GitHub
Unable to update metadata for the organization · Issue #698 · bette...
Describe the bug In the organization plugin, the organization update should be able to update the metadata. However it only prompts to update the name and the slug. To Reproduce Steps to reproduce ...
bekacru
bekacru•3mo ago
hey fix should be released today will ping you here hey @Kazz you can try it on v1.0.9-beta.3
Kazz
KazzOP•3mo ago
Tested it with my project, working perfectly 👌

Did you find this page helpful?