fix(plugin-multi-tenant): ensures redirect route is correctly formatted (#11753)
This commit is contained in:
@@ -6,6 +6,7 @@ import { redirect } from 'next/navigation.js'
|
||||
import { getGlobalViewRedirect } from '../../utilities/getGlobalViewRedirect.js'
|
||||
|
||||
type Args = {
|
||||
basePath?: string
|
||||
collectionSlug: CollectionSlug
|
||||
docID?: number | string
|
||||
globalSlugs: string[]
|
||||
@@ -21,6 +22,7 @@ export const GlobalViewRedirect = async (args: Args) => {
|
||||
const headers = await getHeaders()
|
||||
const redirectRoute = await getGlobalViewRedirect({
|
||||
slug: collectionSlug,
|
||||
basePath: args.basePath,
|
||||
docID: args.docID,
|
||||
headers,
|
||||
payload: args.payload,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export const defaults = {
|
||||
basePath: undefined,
|
||||
tenantCollectionSlug: 'tenants',
|
||||
tenantFieldName: 'tenant',
|
||||
tenantsArrayFieldName: 'tenants',
|
||||
|
||||
@@ -38,6 +38,7 @@ export const multiTenantPlugin =
|
||||
const tenantsArrayTenantFieldName =
|
||||
pluginConfig?.tenantsArrayField?.arrayTenantFieldName || defaults.tenantsArrayTenantFieldName
|
||||
const tenantSelectorLabel = pluginConfig.tenantSelectorLabel || defaults.tenantSelectorLabel
|
||||
const basePath = pluginConfig.basePath || defaults.basePath
|
||||
|
||||
/**
|
||||
* Add defaults for admin properties
|
||||
@@ -320,6 +321,7 @@ export const multiTenantPlugin =
|
||||
incomingConfig.admin.components.actions.push({
|
||||
path: '@payloadcms/plugin-multi-tenant/rsc#GlobalViewRedirect',
|
||||
serverProps: {
|
||||
basePath,
|
||||
globalSlugs: globalCollectionSlugs,
|
||||
tenantFieldName,
|
||||
tenantsCollectionSlug,
|
||||
|
||||
@@ -2,6 +2,14 @@ import type { AcceptedLanguages } from '@payloadcms/translations'
|
||||
import type { ArrayField, CollectionSlug, Field, RelationshipField, User } from 'payload'
|
||||
|
||||
export type MultiTenantPluginConfig<ConfigTypes = unknown> = {
|
||||
/**
|
||||
* Base path for your application
|
||||
*
|
||||
* https://nextjs.org/docs/app/api-reference/config/next-config-js/basePath
|
||||
*
|
||||
* @default undefined
|
||||
*/
|
||||
basePath?: string
|
||||
/**
|
||||
* After a tenant is deleted, the plugin will attempt to clean up related documents
|
||||
* - removing documents with the tenant ID
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import type { Payload, User, ViewTypes } from 'payload'
|
||||
|
||||
import { formatAdminURL } from 'payload/shared'
|
||||
|
||||
import { findTenantOptions } from '../queries/findTenantOptions.js'
|
||||
import { getCollectionIDType } from './getCollectionIDType.js'
|
||||
import { getTenantFromCookie } from './getTenantFromCookie.js'
|
||||
|
||||
type Args = {
|
||||
basePath?: string
|
||||
docID?: number | string
|
||||
headers: Headers
|
||||
payload: Payload
|
||||
@@ -17,6 +20,7 @@ type Args = {
|
||||
}
|
||||
export async function getGlobalViewRedirect({
|
||||
slug,
|
||||
basePath,
|
||||
docID,
|
||||
headers,
|
||||
payload,
|
||||
@@ -31,7 +35,7 @@ export async function getGlobalViewRedirect({
|
||||
payload,
|
||||
})
|
||||
let tenant = getTenantFromCookie(headers, idType)
|
||||
let redirectRoute
|
||||
let redirectRoute: `/${string}` | void = undefined
|
||||
|
||||
if (!tenant) {
|
||||
const tenantsQuery = await findTenantOptions({
|
||||
@@ -65,18 +69,18 @@ export async function getGlobalViewRedirect({
|
||||
if (view === 'document') {
|
||||
if (docID && !tenantDocID) {
|
||||
// viewing a document with an id but does not match the selected tenant, redirect to create route
|
||||
redirectRoute = `${payload.config.routes.admin}/collections/${slug}/create`
|
||||
redirectRoute = `/collections/${slug}/create`
|
||||
} else if (tenantDocID && docID !== tenantDocID) {
|
||||
// tenant document already exists but does not match current route doc ID, redirect to matching tenant doc
|
||||
redirectRoute = `${payload.config.routes.admin}/collections/${slug}/${tenantDocID}`
|
||||
redirectRoute = `/collections/${slug}/${tenantDocID}`
|
||||
}
|
||||
} else if (view === 'list') {
|
||||
if (tenantDocID) {
|
||||
// tenant document exists, redirect to edit view
|
||||
redirectRoute = `${payload.config.routes.admin}/collections/${slug}/${tenantDocID}`
|
||||
redirectRoute = `/collections/${slug}/${tenantDocID}`
|
||||
} else {
|
||||
// tenant document does not exist, redirect to create route
|
||||
redirectRoute = `${payload.config.routes.admin}/collections/${slug}/create`
|
||||
redirectRoute = `/collections/${slug}/create`
|
||||
}
|
||||
}
|
||||
} catch (e: unknown) {
|
||||
@@ -85,5 +89,15 @@ export async function getGlobalViewRedirect({
|
||||
`${typeof e === 'object' && e && 'message' in e ? `e?.message - ` : ''}Multi Tenant Redirect Error`,
|
||||
)
|
||||
}
|
||||
return redirectRoute
|
||||
|
||||
if (redirectRoute) {
|
||||
return formatAdminURL({
|
||||
adminRoute: payload.config.routes.admin,
|
||||
basePath,
|
||||
path: redirectRoute,
|
||||
serverURL: payload.config.serverURL,
|
||||
})
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user