Skip to content

Unable to manage access control for auth fields when more than one collection has auth enabled. #11569

@shiva-hack

Description

@shiva-hack

Describe the Bug

Scenario

  • Two or more auth collections in a payloadcms instance.
  • Here they are members and users.
  • The admin is hooked to the users collection which has two roles user and admin.
  • The members collection has auth fields as well as some extra fields for the member profile.
// payload.config.ts

export default buildConfig({
...
collections: [Members, Users],
...
}) 
// users.collection.ts

export const UsersCollection: CollectionConfig<'users'> = {
  slug: 'users',
  auth: true,
  admin: {
    useAsTitle: 'email',
  },
  fields: [
    {
      name: 'roles',
      label: 'Roles',
      type: 'select',
      options: ['admin', 'user'],
      hasMany: true,
    },
  ],
}
// members.collection.ts

export const MembersCollection: CollectionConfig<'members'> = {
  slug: 'members',
  auth: true,
  admin: {
    useAsTitle: 'email',
  },
  access: {
    create: ({ req: { user } }) =>
      Boolean(user?.collection === 'users' && user.roles?.includes('admin')),
    update: ({ req: { user } }) => {
      // TODO: there is no way to block the payloadcms user only role from editing the auth fields like email and password.
      return Boolean(user?.collection === 'users')
    },
    // TODO: we need something like this to block the payloadcms user only role from updating the auth fields like email and password.
    // auth: ({ req: { user } }) =>
    //   Boolean(user?.collection === 'users' && user.roles?.includes('admin')),
  },
  fields: [
    {
      name: 'name',
      label: 'Name',
      type: 'text',
      required: true,
    },
  ],
}

Expected Behavior

  • The user role from the users collection should not see the auth fields for the member.
  • The admin role from the users collection should see and be able to update the auth fields for the member.

Current Behavior

  • There is no field level / auth group level access control specifically for the CRUD actions on the auth fields.

I have added TODO notes in the members collection

Link to the code that reproduces this issue

https://github.com/shiva-hack/payload/tree/fix/auth-fields-access

Reproduction Steps

  • Create two or more collections with auth enabled.
  • Try to block the user of the admin users collection from accessing the auth fields based on a user role.

Which area(s) are affected? (Select all that apply)

area: ui, area: core

Environment Info

Binaries:
  Node: 22.6.0
  npm: 10.8.2
  Yarn: N/A
  pnpm: 9.7.1
Relevant Packages:
  payload: 3.27.0
  next: 15.2.0
  @payloadcms/db-postgres: 3.27.0
  @payloadcms/live-preview-react: 3.27.0
  react: 19.0.0
  react-dom: 19.0.0
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6020
  Available memory (MB): 16384
  Available CPU cores: 12

Activity

added
status: needs-triagePossible bug which hasn't been reproduced yet
validate-reproductionAuto-added tag on create to tell bot to check recreation URL, removed after check.
on Mar 6, 2025
removed
validate-reproductionAuto-added tag on create to tell bot to check recreation URL, removed after check.
on Mar 6, 2025
jessrynkar

jessrynkar commented on Mar 7, 2025

@jessrynkar
Member

Hey @shiva-hack - sorry if I am misunderstanding your issue but I tested this and using the access.update function does provide the desired behavior that you described:

update: ({ req: { user } }) => {
    const isAdmin = user?.collection === 'users' && user?.roles?.includes('admin') 
    return isAdmin 
},

When the user is not an admin, this is how the auth fields would look on the members collection:
Image
They would not be able to edit the email, change the password or force unlock as these get set to read-only.

What part is not working as you expected?

shiva-hack

shiva-hack commented on Mar 7, 2025

@shiva-hack
Author

@JessChowdhury , so this does check the admin key over the entire collection, but what if we just want to disable the auth fields and keep the other fields open?

  • The user should be able to update other fields except email, change the password or force unlock.
  • The admin should be able to update all the fields including email, change the password and force unlock.
self-assigned this
on Mar 12, 2025
cewald

cewald commented on Mar 24, 2025

@cewald

I have the same problem – i want to give users the option to change single fields but not others email address nor password.

added a commit that references this issue on Jun 25, 2025
1845669
github-actions

github-actions commented on Jun 27, 2025

@github-actions
Contributor

🚀 This is included in version v3.44.0

github-actions

github-actions commented on Jul 5, 2025

@github-actions
Contributor

This issue has been automatically locked.
Please open a new issue if this issue persists with any additional detail.

locked as resolved and limited conversation to collaborators on Jul 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @cewald@shiva-hack@jessrynkar

    Issue actions

      Unable to manage access control for auth fields when more than one collection has auth enabled. · Issue #11569 · payloadcms/payload