Let's say there are two collections - Questions and Answers.
And also two types of users - Admin and Simple.
Is it possible to leave a collection of Answers available for reading and writing (data is written to it from the Questions collection), but hide its visibility for Simple.
If you manage visibility from access, then the data from the Questions will not be recorded in the Answers.
Perhaps there is an option to somehow conditionally connect css.
You can use the
hidden
property on your collection. Set it to a function that reads the userβs role and returns a boolean.
Thank you!! I know and I tried, but it doesn't work for me.. it also hide collection and for 'admin' with no reason π
I've a similar use case, the user object only returns the following even though my user collection has many other fields
collection:"users",
email:"admin@test.com",
exp:1685879942,
iat: 1685872742,
id: "6479c376892b35f827fcc2f3"
You need to add a
saveToJWT
property to any fields you wish to save on your authenticated user
As for the
hidden
function, it should work as Iβm describing but if not, there could be a bug
@chistayaaa I only last week was looking for an answer to the same question.
As @jacobsfletch mentioned, you can leverage the
admin.hidden?
property.
The following is how I hide collections from users other than admins;
hidden: ({ user }) => !(user as any).roles.includes('admin')
I'm not a fan of using an "any" type, but I haven't worked out how to leverage my User type yet.
Hey, Chris! Thank you for your example!! I tried to use ternary operator, but it doesn't work.. and I did what you did and everything works! Thanks π
@chistayaaa you're welcome. A better version, is below. It doesn't rely on an
any
type.
#friendsdon'tLetFriendsUseAny
import { User as AuthUser } from 'payload/dist/auth/types';
import { CollectionConfig } from 'payload/types';
import { User } from '../payload-types';
type CustomUser = {
user: User & AuthUser;
};
export const PpeMedia: CollectionConfig = {
slug: 'ppe-media',
labels: {
singular: 'PPE Media',
plural: 'PPE Media',
},
admin: {
group: 'Hidden',
hidden: ({ user }: CustomUser) => !user.roles.includes('admin'),
hideAPIURL: true,
},
access: {
read: () => true,
},
fields: [],
upload: {
staticURL: '/ppe-media',
staticDir: 'media/ppe-media',
mimeTypes: ['image/*'],
resizeOptions: {
width: 100,
height: 100,
},
formatOptions: {
format: 'webp',
options: {
lossless: true,
},
},
},
};
wow, looks cool, thanks! π
This doesn't work in strict mode π
I'm wasn't aware strict mode played a role in this logic. What sort of error are you getting?
Type '({ user }: CustomUser) => boolean' is not assignable to type 'boolean | ((args: { user: User; }) => boolean) | undefined'.
Type '({ user }: CustomUser) => boolean' is not assignable to type '(args: { user: User; }) => boolean'.
Types of parameters '__0' and 'args' are incompatible.
Type '{ user: User; }' is not assignable to type 'CustomUser'.
Types of property 'user' are incompatible.
Type 'User' is not assignable to type 'User & AuthUser'.
Type 'User' is missing the following properties from type 'AuthUser': name, role, locale, updatedAt, createdAt
Please note: AuthUser is the users collection and User is the payload's user type
Can you post your imports and your customUser type and your admin.hidden?
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.