Skip to content

[Bug]: Unexpected role transformation for collection with 'role' field. #112

@luochuanyuewu

Description

@luochuanyuewu

Description

In the normalizeData function located in "transform/index.ts", you convert any input with a role field to [role].
This is desirable for the user role; however, the member and invitation collections also have a role field.
When the payload adapter creates a new member and an invitation, it will cause a payload error.
I have to use CollectionOverride to add the following beforechange hook to the role field of member and invitation to revert ['owner', 'admin'] back to the form "owner, admin" to make this error disappear.

Steps To Reproduce

1.configure the better-auth-plugin options with organization and team enabled.
2.create new org with auth-client, then the default member(for org owner) failed to create.

Here is my before change hook to solve this error:
const transformRoleHook: FieldHook = ({ value, operation }) => { if ( (operation === 'create' || operation === 'update') && Array.isArray(value) ) { // convert back from ['owner','admin'] to 'owner,admin' return value.join(',') } return value }
Here is how i do the collection override:
` members: ({ collection }) => {
const updatedFields = collection.fields.map(field => {
if ('name' in field && field.name === 'role' && field.type == 'text') {
return {
...field,
hooks: {
...field.hooks,
beforeChange: [transformRoleHook],
},
}
}
return field
})

  return {
    ...collection,
    admin: {
      ...COLLECTION_GROUP_ORG,
    },
    labels: {
      plural: { en: 'Organization Members', zh: '组织成员' },
      singular: { en: 'organization Member', zh: '组织成员' },
    },
    fields: updatedFields,
  }
},`

Expected Behavior

When a user creates a new organization, the current user will be the owner when creating a member for that organization. The role field when creating an organization should be 'owner' instead of ['owner'].

Environment

Payload Auth Version:1.7.1
Payload Version:3.65.0
Next Js: v16.0.1

Reproduction URL (optional)

No response

Additional Context

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions