Hi,
I'm making a collection access control function and want to check if the user is a manager of the organization being udpated. I am getting the error that findById was not found, despite it working elsewhere and having checks for the req.
The error happens only when I am logging in. If I comment out the code and log in first, and then use it, things work as they should.
import { Access } from "payload/types";
export const isManagerOfOrganizationOrAdmin: Access = async ({ req, id }) => {
if (!req || !req.user) return false;
if (req?.user?.roles?.includes('admin')) return true;
const organization = await req?.payload?.findByID({
collection: 'organizations',
id,
depth: 0,
});
// If the user is a manager of the organization
return organization?.managers?.includes(req.user.id);
}
export const Organizations: CollectionConfig = {
slug: 'organizations',
admin: {
useAsTitle: 'name',
},
access: {
create: isAdmin,
update: isManagerOfOrganizationOrAdmin,
read: () => true,
},
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.