Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Get data user ID

default discord avatar
igormfelast year

Hi,



I want to make a hook that enforces a certain field array to be empty if a role is removed from a user. Only admins can remove roles, so the req.user.id cannot be used, since I want to remove the roles from the target user not the admin.



I want to achieve something like this:



export const enforceFacilitator: FieldHook<User & { id: string }> = async ({ req, data }) => { const isFacilitator = targetUser.roles.includes('facilitator'); if (!isFacilitator) { await payload.update({ collection: 'users', id: targetUser.id, data: { array: [], }, }); } }

If possible, it would be great to check if the role of facilitator was removed instead of simply checking if it is there or not (to avoid unnecessary operations). I could just check if the array is already empty before making the update though, which achieves a similar result but isn't very organized.

    Star on GitHub

    Star

    Chat on Discord

    Discord

    online

    Can't find what you're looking for?

    Get dedicated engineering support directly from the Payload team.