Can we hide the visibility of the collection without setting access?

default discord avatar
chistayaaa
3 months ago
11

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.

  • discord user avatar
    jacobsfletch
    Payload Team
    3 months ago

    You can use the

    hidden

    property on your collection. Set it to a function that reads the user’s role and returns a boolean.

  • default discord avatar
    chistayaaa
    3 months ago

    Thank you!! I know and I tried, but it doesn't work for me.. it also hide collection and for 'admin' with no reason πŸ™‚

  • default discord avatar
    yhn5790
    3 months ago

    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"
  • discord user avatar
    jacobsfletch
    Payload Team
    3 months ago

    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

  • default discord avatar
    _itsjustchris
    3 months ago

    @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.

  • default discord avatar
    chistayaaa
    3 months ago

    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 πŸ™‚

  • default discord avatar
    _itsjustchris
    3 months ago

    @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, }, }, }, };
  • default discord avatar
    chistayaaa
    3 months ago

    wow, looks cool, thanks! πŸ˜ƒ

  • default discord avatar
    yhn5790
    3 months ago

    This doesn't work in strict mode 😐

  • default discord avatar
    _itsjustchris
    3 months ago

    I'm wasn't aware strict mode played a role in this logic. What sort of error are you getting?

  • default discord avatar
    yhn5790
    3 months ago
    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

  • default discord avatar
    _itsjustchris
    3 months ago

    Can you post your imports and your customUser type and your admin.hidden?

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.