Files
payload/test/admin/components/views/CustomProtectedView/index.tsx
Paul 01ccbd48b0 feat!: custom views are now public by default and fixed some issues with notFound page (#8820)
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
2024-10-30 11:29:29 -06:00

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>
&nbsp; &nbsp; &nbsp;
<Button
buttonStyle="secondary"
el="link"
Link={Link}
to={`${adminRoute}/${customViewPath}`}
>
Go to Custom View
</Button>
</div>
</div>
)
}