multi-tenant: tenant selection (payload-tenant cookie) deleted on every hard reload for userHasAccessToAllTenants users
Describe the Bug
For a super-admin (or any user whose tenant list comes from userHasAccessToAllTenants), the tenant selected in the admin sidebar's tenant selector is lost on every hard page reload. Client-side navigation keeps it; F5 / direct URL load wipes it, on every admin view (native collection lists, custom views).
Root cause (traced with a temporary log inside TenantSelectionProvider)
On a hard document load, the admin RootLayout can render the plugin's TenantSelectionProvider (registered via admin.components.providers) with a null user, even though the browser sent the auth cookie — page-level renders of the same request authenticate fine. Trace from the server provider (dist/providers/TenantSelectionProvider/index.js) on a single F5:
[probe] user= null cookie= fa7a3be9-… options= 0 initialValue= undefined <- first pass
[probe] user= nestor@… cookie= undefined options= 3 initialValue= undefined <- cookie already deleted
Chain:
- Blind pass:
user = null→getTenantOptions()returns[]→ the (valid!)payload-tenantcookie matches no option →initialValue = undefined. index.client.jshas a cleanup effect:if (!initialValue) setTenant({ id: undefined, refresh: true })— intended for stale cookies. On the blind pass it fires and deletes the user's cookie.- Subsequent renders have the real user, but the selection is gone.
A side observation: the admin shell for that blind pass renders empty, and it currently only re-paints because this destructive path happens to call router.refresh() — so the cleanup can't simply be skipped, the refresh is load-bearing.
Suggested fix (verified locally via pnpm patch)
In TenantSelectionProviderClient, split the !initialValue case:
if (!initialValue) {
if (initialTenantOptions.length > 0) {
// server actually had options to judge with -> genuine stale cookie, original cleanup
setTenant({ id: undefined, refresh: true })
} else if (!retriedBlindPass.current) {
// blind (userless) pass -> keep the cookie, refresh once so the next
// authenticated render paints and restores the selection
retriedBlindPass.current = true
router.refresh()
}
}
plus adopting a later-arriving initialValue into selectedTenantID when none is selected (the useState(initialValue) keeps the stale undefined after the refresh, so the selector renders blank while the tenant is actually active).
With this, selection survives hard reloads; genuine stale-cookie cleanup still works; the useRef cap prevents any refresh loop for logged-out visitors with leftover cookies. (First naive attempt — only guarding the delete without refreshing — leaves the admin blank, confirming the refresh is required on the blind pass.)
The deeper question is why the RootLayout provider pass has req.user = null on document loads while page renders of the same request authenticate — the plugin fix above is defensive either way (don't destroy state based on a userless render).
To Reproduce
- Multi-tenant plugin with
userHasAccessToAllTenants: (user) => user?.role === 'super-admin', 2+ tenants. - Log in as super-admin, pick a tenant in the sidebar selector.
- Hard-reload any admin page (F5).
- Selector is back to "Select a value";
payload-tenantcookie is gone.
Payload Version
3.86.0 (plugin @payloadcms/plugin-multi-tenant 3.86.0 — 3.87.0 dist has identical provider code)
Adapters and Plugins
@payloadcms/plugin-multi-tenant, @payloadcms/db-postgres, Next.js 16.2.11 (Turbopack dev), Node 24
Hey @NestorCanales! Thanks for the detailed issue description. I have gone ahead and opened a PR to fix this behavior exactly as you suggested. You can check it out here: #17691