feat: refine admin workflows and deployment packaging
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import configPromise from '@payload-config'
|
||||
import { RootPage, generatePageMetadata } from '@payloadcms/next/views'
|
||||
import { headers as getHeaders } from 'next/headers'
|
||||
import { redirect } from 'next/navigation'
|
||||
import { notFound, redirect } from 'next/navigation'
|
||||
import { executeAuthStrategies, getPayload } from 'payload'
|
||||
// @ts-expect-error generated by Payload admin import map generator
|
||||
import { importMap } from '../importMap.js'
|
||||
@@ -13,6 +13,11 @@ type AdminPageProps = {
|
||||
searchParams: Promise<{ [key: string]: string | string[] | undefined }>
|
||||
}
|
||||
|
||||
async function getAdminSegments(params: AdminPageProps['params']) {
|
||||
const value = await params
|
||||
return Array.isArray(value.segments) ? value.segments.filter(Boolean) : []
|
||||
}
|
||||
|
||||
async function getAdminUser() {
|
||||
const [config, headers] = await Promise.all([configPromise, getHeaders()])
|
||||
const payload = await getPayload({
|
||||
@@ -28,23 +33,23 @@ async function getAdminUser() {
|
||||
}
|
||||
|
||||
export async function generateMetadata({ params, searchParams }: AdminPageProps) {
|
||||
const segments = await getAdminSegments(params)
|
||||
|
||||
if (segments[0] === 'forgot') notFound()
|
||||
|
||||
return generatePageMetadata({
|
||||
config: configPromise,
|
||||
params: params as Promise<{ [key: string]: string | string[] }>,
|
||||
params: Promise.resolve({ segments }),
|
||||
searchParams: searchParams as Promise<{ [key: string]: string | string[] }>,
|
||||
})
|
||||
}
|
||||
|
||||
export default async function PayloadAdminPage({ params, searchParams }: AdminPageProps) {
|
||||
const normalizedParams = params.then(async (value) => {
|
||||
const segments = Array.isArray(value.segments) ? value.segments.filter(Boolean) : []
|
||||
|
||||
return { segments }
|
||||
})
|
||||
|
||||
const { segments } = await normalizedParams
|
||||
const segments = await getAdminSegments(params)
|
||||
const resolvedSearchParams = await searchParams
|
||||
|
||||
if (segments[0] === 'forgot') notFound()
|
||||
|
||||
if (segments.length === 1 && segments[0] === 'login') {
|
||||
if (await getAdminUser()) redirect('/admin')
|
||||
|
||||
@@ -52,9 +57,10 @@ export default async function PayloadAdminPage({ params, searchParams }: AdminPa
|
||||
}
|
||||
|
||||
if (segments.length === 0) {
|
||||
if (!(await getAdminUser())) redirect('/admin/login?redirect=/admin')
|
||||
const user = await getAdminUser()
|
||||
if (!user) redirect('/admin/login?redirect=/admin')
|
||||
|
||||
return <AdminHome cacheCleared={resolvedSearchParams.cache === 'cleared'} />
|
||||
return <AdminHome cacheCleared={resolvedSearchParams.cache === 'cleared'} user={user} />
|
||||
}
|
||||
|
||||
return RootPage({
|
||||
|
||||
Reference in New Issue
Block a user