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,
            };
        },
Was this page helpful?