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.

Accessing User Preferences in Access Control Function

default discord avatar
notchrlast year
5

Are you able to use react hooks?



https://payloadcms.com/docs/admin/hooks#usepreferences

@434476210630950912
  • default discord avatar
    mykz_last year

    I don't think you can within an access control function right?



    As the access control functions are run on the server and you can't use react hooks inside of server components from my understanding.



    Yeah, you can't because access controls are not Components.

  • default discord avatar
    notchrlast year

    Ah



    But you can make a REST call



    https://payloadcms.com/docs/rest-api/overview#preferences

    try {
      const req = await fetch('{cms-url}/api/payload-preferences/{key}', {
        method: "GET", 
        credentials: "include",
        headers: {
          "Content-Type": "application/json",
        },
      })
      const data = await req.json()
    } catch (err) {
      console.log(err)
    }
  • default discord avatar
    mykz_last year

    Yeah, I was thinking I could use the local api but I wasn't sure of the collection name.

  • default discord avatar
    notchrlast year

    Seems promising

  • default discord avatar
    mykz_last year

    I'll give it a try and see 🙂



    Sorted it, should be:


    const preferences = await payload.find({
      collection: 'payload-preferences',
      req: args.req,
      where: {
        key: {
          equals: 'the-key',
        },
      },
    })


    Thanks for the help

    @486710146651652106

    o/

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.