Skip to content

Commit 10a8f0f

Browse files
authored
fix(next): unhandled error in renderListView server function when no user present (#14878)
Previously, the following error may be thrown when no `user` is present on the `req`: ```ts TypeError: Cannot read properties of null (reading 'collection') at renderListHandler (packages/next/src/views/List/handleServerFunction.tsx:66:28) at async Server.<anonymous> (test/dev.ts:129:5) 64 | { 65 | 'user.relationTo': { > 66 | equals: user.collection, | ^ 67 | }, 68 | }, 69 | { { digest: '4059983158' } POST /admin/collections/with-list-drawer 500 in 164ms (compile: 2ms, render: 162ms) ``` I'm seeing it thrown a lot in our test suites, likely because it's logging out the user while a previous request is still going on. This PR explicitly throws an `UnauthorizedError` before the server function tries to access properties on the `user` object.
1 parent c9a8aa0 commit 10a8f0f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

packages/next/src/views/List/handleServerFunction.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import type { CollectionPreferences, ServerFunction, VisibleEntities } from 'pay
33

44
import { getClientConfig } from '@payloadcms/ui/utilities/getClientConfig'
55
import { headers as getHeaders } from 'next/headers.js'
6-
import { canAccessAdmin, getAccessResults, isEntityHidden, parseCookies } from 'payload'
6+
import {
7+
canAccessAdmin,
8+
getAccessResults,
9+
isEntityHidden,
10+
parseCookies,
11+
UnauthorizedError,
12+
} from 'payload'
713
import { applyLocaleFiltering } from 'payload/shared'
814

915
import { renderListView } from './index.js'
@@ -33,6 +39,10 @@ export const renderListHandler: ServerFunction<
3339
},
3440
} = args
3541

42+
if (!req.user) {
43+
throw new UnauthorizedError()
44+
}
45+
3646
const headers = await getHeaders()
3747

3848
const cookies = parseCookies(headers)

0 commit comments

Comments
 (0)