payload icon indicating copy to clipboard operation
payload copied to clipboard

Cannot prevent interaction with "Enable API Key" field

Open erikmartus opened this issue 4 months ago • 1 comments

Describe the Bug

When the API Key Strategy is used on a collection, it's not possible, or at least not clear, to prevent users from interacting with the "Enable API Key" field. In the most extreme example completely blocking update access on the collection does not prevent interaction to this field like others:

access: {
  update: () => false,
},

While users without update access can't actually save the change by toggling the field, it's confusing that the interaction is even possible given everything else is disabled.

Link to the code that reproduces this issue

https://github.com/erikmartus/payload-api-field

Reproduction Steps

  1. After starting to the application, navigate to the Users collection and open any user.
  2. Click on the checkbox for the "Enable API Key" field, verify that it toggles displaying the API Key field.
  3. Attempt to edit any other field, verify that the field is disabled.
  4. Navigate away or refresh the page to verify that the change is not persisted. Image
Image

Which area(s) are affected?

area: ui

Environment Info

Binaries:
  Node: 25.9.0
  npm: 11.12.1
  Yarn: 1.22.22
  pnpm: N/A
Relevant Packages:
  payload: 3.85.2
  next: 16.2.11
  @payloadcms/db-postgres: 3.85.2
  @payloadcms/drizzle: 3.85.2
  @payloadcms/graphql: 3.85.2
  @payloadcms/live-preview: 3.86.0
  @payloadcms/live-preview-react: 3.86.0
  @payloadcms/next/utilities: 3.85.2
  @payloadcms/plugin-cloud-storage: 3.85.2
  @payloadcms/richtext-lexical: 3.85.2
  @payloadcms/sdk: 3.85.2
  @payloadcms/storage-s3: 3.85.2
  @payloadcms/translations: 3.85.2
  @payloadcms/ui/shared: 3.85.2
  react: 19.2.7
  react-dom: 19.2.7
Operating System:
  Platform: linux
  Arch: arm64
  Version: #1 SMP Thu Jun 11 15:44:51 UTC 2026
  Available memory (MB): 3919
  Available CPU cores: 4

erikmartus avatar Jul 27 '26 17:07 erikmartus

I tried to write a failing test for this against current main and couldn't make the checkbox interactive. Posting the negative result since it narrows things down.

I added a collection to test/auth matching the report — useAPIKey: true plus collection-level access: { update: () => false } — and opened an existing document in the admin. Dumped the rendered DOM rather than trusting an assertion:

<div class="field-type checkbox checkbox--checked checkbox--read-only">
  <input id="field-enableAPIKey" type="checkbox" disabled="" checked="">

disabled and checkbox--read-only are both already there. Same result with disableLocalStrategy: true and with the local strategy enabled — in the second case #field-email and the change-password / force-unlock buttons come back disabled too, so the read-only state is being applied consistently across the auth block.

One code-level note that may or may not be related. views/Edit/Auth/index.tsx passes the read-only state inside the field object:

<CheckboxField
  field={{ name: 'enableAPIKey', admin: { disabled, readOnly: enableAPIKeyReadOnly }, ... }}
  path="enableAPIKey"
/>

but CheckboxFieldComponent reads readOnly off its props, not off field.admin, and its disabled comes from useField (which is processing || initializing). So field.admin.readOnly and field.admin.disabled are both inert there. It isn't what's disabling the checkbox in my runs — something else in the auth block is — but it does mean enableAPIKeyReadOnly isn't reaching the component, which could matter for the field-level-permission case (apiKeyPermissions?.update === false with collection update allowed), which I did not test.

What I did not do: run https://github.com/erikmartus/payload-api-field. I built the scenario from the description instead, so if the repro depends on something specific to that project's config I would have missed it. If you can share the Payload version it still reproduces on, or confirm it against main, that would settle whether this is already fixed.

luantaraschi avatar Aug 06 '26 22:08 luantaraschi