Role checks in client components: export them from /client, fix the docs
access.md and the 1.16.4 upgrade notes led projects to import isAdmin from
'@intecion/ipal-kit' in a client component; with `next build --webpack` that
fails ('server-only' cannot be imported from a Client Component module).
No existing entry had a client-safe one, so '@intecion/ipal-kit/client' now
exports hasMinimumRole, isAdmin, isEditor and the type Role; the main entry
keeps them for server code.
Docs: access.md (snippet with imports, server vs client, useConfig() carries
admin.user only), AGENT.md, ipal-debug, ipal-reference, ipal-upgrade, the
1.16.4 note, an Unreleased changelog entry. Tests: /client reaches no
server-only module, the predicates' answers with a client config, no
client-component example in the docs imports the main entry.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
1270ebc3d1
commit
de414a81a8
+19
-4
@@ -135,16 +135,31 @@ which collections are staff:
|
||||
| Where you call it | Pass | Without it |
|
||||
|---|---|---|
|
||||
| access functions, hooks, endpoints (server, the config is loaded) | `req.payload` | works: the staff collections of the config `ipalKit` built in this process. With two different configs in one process, only collections that are staff in **both** count — pass `req.payload` to get that config's own answer |
|
||||
| client components | the config from `useConfig()` (it has `admin.user`), or `{ staffCollections: ['users', …] }` | **always `false`** — the browser bundle has no config |
|
||||
| client components — import from **`@intecion/ipal-kit/client`** | the config from `useConfig()` (it has `admin.user`, the panel's login collection), or `{ staffCollections: ['users', …] }` | **always `false`** — the browser bundle has no config |
|
||||
| Next middleware / `proxy.ts`, the edge runtime | `{ staffCollections: ['users'] }` | **always `false`** — the config is not loaded there |
|
||||
|
||||
```tsx
|
||||
'use client'
|
||||
const { config } = useConfig()
|
||||
const { user } = useAuth()
|
||||
if (isAdmin(user, config)) { /* show the button — a hint only, the server decides */ }
|
||||
import { isAdmin } from '@intecion/ipal-kit/client' // not '@intecion/ipal-kit'
|
||||
import { useAuth, useConfig } from '@payloadcms/ui'
|
||||
|
||||
export function AdminOnlyHint() {
|
||||
const { config } = useConfig()
|
||||
const { user } = useAuth()
|
||||
// a hint only — the server decides
|
||||
return isAdmin(user, config) ? <p>…</p> : null
|
||||
}
|
||||
```
|
||||
|
||||
- **The import in a client component is `/client`** (`hasMinimumRole`, `isAdmin`, `isEditor`, the type `Role`). The main
|
||||
entry reaches `server-only` modules: with `next build --webpack` it fails with `'server-only' cannot be imported from
|
||||
a Client Component module` (Turbopack builds it, but the import does not belong there either).
|
||||
In 1.16.4–1.16.6 `/client` does not have them — upgrade.
|
||||
- **On the server** (config, hooks, access functions, endpoints, server components, `proxy.ts`) import from
|
||||
`@intecion/ipal-kit`: `/client` is a `'use client'` entry, a server component that calls a function from it fails.
|
||||
- `useConfig()` carries `admin.user` only (Payload strips `config.custom` from the client config). Accounts of
|
||||
`access.staffCollections` need the list: `isAdmin(user, { staffCollections: ['users', 'partners'] })`.
|
||||
|
||||
They fail closed: where the staff collections are not known, nobody is staff, and an account without
|
||||
`collection` is nobody. `isAdmin` and the others do **not** check the second factor; `userMeetsRole` and every
|
||||
`access` function in the config do.
|
||||
|
||||
Reference in New Issue
Block a user