Skip to content

Commit a9c0832

Browse files
fix(plugin-multi-tenant): prevent throwing when no user exists (#10699)
### What? Fixes issue where the provider would throw an error and prevent the login screen from loading if there was no user. ### Why? Missing try/catch around tenant find for the provider. (Missed because test suites have autoLogin: true) ### How? Adds try/catch around find query.
1 parent 3e0baf5 commit a9c0832

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

examples/multi-tenant/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"dependencies": {
1919
"@payloadcms/db-mongodb": "latest",
2020
"@payloadcms/next": "latest",
21-
"@payloadcms/plugin-multi-tenant": "file:payloadcms-plugin-multi-tenant-3.15.1.tgz",
21+
"@payloadcms/plugin-multi-tenant": "latest",
2222
"@payloadcms/richtext-lexical": "latest",
2323
"@payloadcms/ui": "latest",
2424
"cross-env": "^7.0.3",

packages/plugin-multi-tenant/src/providers/TenantSelectionProvider/index.client.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import type { OptionObject } from 'payload'
44

5+
import { useAuth } from '@payloadcms/ui'
56
import { useRouter } from 'next/navigation.js'
67
import React, { createContext } from 'react'
78

@@ -34,6 +35,8 @@ export const TenantSelectionProviderClient = ({
3435
initialValue || SELECT_ALL,
3536
)
3637
const [preventRefreshOnChange, setPreventRefreshOnChange] = React.useState(false)
38+
const { user } = useAuth()
39+
const userID = React.useMemo(() => user?.id, [user?.id])
3740

3841
const router = useRouter()
3942

@@ -72,6 +75,10 @@ export const TenantSelectionProviderClient = ({
7275
}
7376
}, [initialValue, setTenant, selectedTenantID, tenantOptions])
7477

78+
React.useEffect(() => {
79+
router.refresh()
80+
}, [userID, router])
81+
7582
return (
7683
<Context.Provider
7784
value={{

packages/plugin-multi-tenant/src/providers/TenantSelectionProvider/index.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,26 @@ export const TenantSelectionProvider = async ({
1919
useAsTitle,
2020
user,
2121
}: Args) => {
22-
const { docs: userTenants } = await payload.find({
23-
collection: tenantsCollectionSlug,
24-
depth: 0,
25-
limit: 1000,
26-
overrideAccess: false,
27-
sort: useAsTitle,
28-
user,
29-
})
30-
31-
const tenantOptions: OptionObject[] = userTenants.map((doc) => ({
32-
label: String(doc[useAsTitle]),
33-
value: String(doc.id),
34-
}))
22+
let tenantOptions: OptionObject[] = []
23+
24+
try {
25+
const { docs: userTenants } = await payload.find({
26+
collection: tenantsCollectionSlug,
27+
depth: 0,
28+
limit: 1000,
29+
overrideAccess: false,
30+
sort: useAsTitle,
31+
user,
32+
})
33+
34+
tenantOptions = userTenants.map((doc) => ({
35+
label: String(doc[useAsTitle]),
36+
value: String(doc.id),
37+
}))
38+
} catch (_) {
39+
// user likely does not have access
40+
}
41+
3542
const cookies = await getCookies()
3643
const tenantCookie = cookies.get('payload-tenant')?.value
3744
const selectedTenant =

0 commit comments

Comments
 (0)