REST API - Basic Auth

default discord avatar
bensler12 months ago
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
    derosul12 months ago

    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
    bensler12 months ago

    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
    derosul12 months ago

    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
    bensler12 months ago

    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.