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.

REST API - Basic Auth

default discord avatar
benslerlast year
4

Hello,



Is it possible to have a collection with only admin access, that i can fetch data from using basic auth with my admins credentials and the rest api?

  • default discord avatar
    derosullast year

    Not entirely sure about using basic auth, but perhaps this might fit your need:



    https://payloadcms.com/docs/authentication/config#api-keys


    Simply create an API key for your admin user, and use the created api key to authenticate 🙂

  • default discord avatar
    benslerlast year

    So i would just do this:



    const Users: CollectionConfig = {
      slug: 'users',
      admin: {
        useAsTitle: 'name',
        defaultColumns: ['name', 'email'],
      },
      access: {
        read: admins,
        create: anyone,
        update: adminsAndUser,
        delete: admins,
        // admin: ({ req: { user } }) => checkRole(['admin'], user),
      },
      auth: {
        useAPIKey: true,
      },
      fields: UserFields,
      timestamps: true,
    }


    On my users config, and then i will be able to authenticate to all my collections, after checking the checkbox on the users page?

  • default discord avatar
    derosullast year

    Yup





    This will appear on a user, it will generate an API key



    And here I use the Authorization header



    And it allows me to create a new document entry

    Screenshot_2023-07-07_at_12.21.43.png
    Screenshot_2023-07-07_at_12.22.08.png
  • default discord avatar
    benslerlast year

    It works, thank you very much 🙂

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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