How to get entire user object? #420
-
I have a collection of admin users with email, name and role properties modeled after the example in the payload-demo. fields: [
// Email added by default
{
name: 'name',
type: 'text',
},
{
name: 'roles',
label: 'Role(s)',
type: 'select',
options: roles,
required: true,
saveToJWT: true,
hasMany: true,
}
], The problem is, that I can not access the role property in the req.user object that is passed to the access properties. It only contains name and collection. What do I have to do to pass the entire user object to the req object? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Interesting, you shouldn't have to do anything extra to pass the full user object in an access function. I would try:
If you want to share a repo with me I'll try it out too. You probably saw in the demo where we have our access function example. https://github.com/payloadcms/payload/blob/master/demo/collections/Admin.ts#L5 const access = ({ req: { user } }) => {
return checkRole(['admin'], user);
}; With that we call checkRole: https://github.com/payloadcms/payload/blob/master/demo/access/checkRole.ts const checkRole = (allRoles, user) => {
if (user) {
if (allRoles.some((role) => user.roles && user.roles.some((individualRole) => individualRole === role))) {
return true;
}
} We're able to use the Is that helpful? If not we'll get you on track. |
Beta Was this translation helpful? Give feedback.
Interesting, you shouldn't have to do anything extra to pass the full user object in an access function. I would try:
If you want to share a repo with me I'll try it out too.
You probably saw in the demo where we have our access function example. https://github.com/payloadcms/payload/blob/master/demo/collections/Admin.ts#L5
With that we call checkRole: https://github.com/payloadcms/payload…