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.

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

default discord avatar
chistayaaa2 years ago
12

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
    2 years 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
    chistayaaa2 years 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
    yhn57902 years 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
    2 years 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
    _itsjustchris2 years ago
    @1106190928152121385

    I only last week was looking for an answer to the same question.



    As

    @808734492645785600

    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
    chistayaaa2 years 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
    _itsjustchris2 years ago
    @1106190928152121385

    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
    chistayaaa2 years ago

    wow, looks cool, thanks! 😃

  • default discord avatar
    yhn57902 years ago

    This doesn't work in strict mode 😐

  • default discord avatar
    _itsjustchris2 years ago

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

  • default discord avatar
    yhn57902 years 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
    _itsjustchris2 years ago

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

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.