Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-multi-tenant): incorrect tenant selection with postgres #10992

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const TenantSelector = ({ viewType }: { viewType?: ViewTypes }) => {
selectedTenantID
? selectedTenantID === SELECT_ALL
? undefined
: String(selectedTenantID)
: (selectedTenantID as string)
: undefined
}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const TenantSelectionProvider = async ({
})
tenantOptions = docs.map((doc) => ({
label: String(doc[useAsTitle]),
value: String(doc.id),
value: doc.id,
}))
} catch (_) {
// user likely does not have access
Expand All @@ -42,15 +42,17 @@ export const TenantSelectionProvider = async ({
const cookies = await getCookies()
let tenantCookie = cookies.get('payload-tenant')?.value
let initialValue = undefined
const isValidTenantCookie =
(tenantOptions.length > 1 && tenantCookie === SELECT_ALL) ||
tenantOptions.some((option) => option.value === tenantCookie)

if (isValidTenantCookie) {
initialValue = tenantCookie
if (tenantOptions.length > 1 && tenantCookie === SELECT_ALL) {
initialValue = SELECT_ALL
} else {
tenantCookie = undefined
initialValue = tenantOptions.length > 1 ? SELECT_ALL : tenantOptions[0]?.value
const matchingOption = tenantOptions.find((option) => String(option.value) === tenantCookie)
if (matchingOption) {
initialValue = matchingOption.value
} else {
tenantCookie = undefined
initialValue = tenantOptions.length > 1 ? SELECT_ALL : tenantOptions[0]?.value
}
}

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseCookies } from 'payload'
import { isNumber } from 'payload/shared'

/**
* A function that takes request headers and an idType and returns the current tenant ID from the cookie
Expand All @@ -13,5 +14,9 @@ export function getTenantFromCookie(
): null | number | string {
const cookies = parseCookies(headers)
const selectedTenant = cookies.get('payload-tenant') || null
return selectedTenant ? (idType === 'number' ? parseFloat(selectedTenant) : selectedTenant) : null
return selectedTenant
? idType === 'number' && isNumber(selectedTenant)
? parseFloat(selectedTenant)
: selectedTenant
: null
}