This PR aims to fix a few issues with the notFound page and custom views so it matches v2 behaviour: - Non authorised users should always be redirected to the login page regardless if not found or valid URL - Previously notFound would render for non users too potentially exposing valid but protected routes and creating a confusing workflow as the UI was being rendered as well - Custom views are now public by default - in our `admin` test suite, the `/admin/public-custom-view` is accessible to non users but `/admin/public-custom-view/protected-nested-view` is not unless the checkbox is true in the Settings global, there's e2e coverage for this - Fixes https://github.com/payloadcms/payload/issues/8716
70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import type { AdminViewProps } from 'payload'
|
|
|
|
import { Button } from '@payloadcms/ui'
|
|
import LinkImport from 'next/link.js'
|
|
import { notFound, redirect } from 'next/navigation.js'
|
|
import React from 'react'
|
|
|
|
import { customNestedViewTitle, customViewPath } from '../../../shared.js'
|
|
import { settingsGlobalSlug } from '../../../slugs.js'
|
|
|
|
const Link = (LinkImport.default || LinkImport) as unknown as typeof LinkImport.default
|
|
|
|
export const CustomProtectedView: React.FC<AdminViewProps> = async ({ initPageResult }) => {
|
|
const {
|
|
req: {
|
|
payload: {
|
|
config: {
|
|
routes: { admin: adminRoute },
|
|
},
|
|
},
|
|
user,
|
|
},
|
|
req,
|
|
} = initPageResult
|
|
|
|
const settings = await req.payload.findGlobal({
|
|
slug: settingsGlobalSlug,
|
|
})
|
|
|
|
if (!settings?.canAccessProtected) {
|
|
if (user) {
|
|
redirect(`${adminRoute}/unauthorized`)
|
|
} else {
|
|
notFound()
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div
|
|
style={{
|
|
marginTop: 'calc(var(--base) * 2)',
|
|
paddingLeft: 'var(--gutter-h)',
|
|
paddingRight: 'var(--gutter-h)',
|
|
}}
|
|
>
|
|
<h1 id="custom-view-title">{customNestedViewTitle}</h1>
|
|
<p>This custom view was added through the Payload config:</p>
|
|
<ul>
|
|
<li>
|
|
<code>components.views[key].Component</code>
|
|
</li>
|
|
</ul>
|
|
<div className="custom-view__controls">
|
|
<Button buttonStyle="secondary" el="link" Link={Link} to={`${adminRoute}`}>
|
|
Go to Dashboard
|
|
</Button>
|
|
|
|
<Button
|
|
buttonStyle="secondary"
|
|
el="link"
|
|
Link={Link}
|
|
to={`${adminRoute}/${customViewPath}`}
|
|
>
|
|
Go to Custom View
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|