Authentication (Basic)

Using Basic Auth for Authentication


The Basic authentication is relatively easy to set up. It requires less configuration and uses username and password as credentials.

1. Create Role

  1. Visit `/admin/people/roles`.
  2. Click on + Add role.
  3. Fill in the Role name. Example: Next.js Site.

2. Assign Permissions

Next, assign the following permissions to the newly created role.

  • Bypass content access control
  • Issue subrequests
  • View user information

We are assigning the Bypass content access control permission to allow Next.js to access unpublished content and revisions.

You can safely skip this permission if you do not need to preview unpublished content.

3. Create User

Add a new user at `/admin/people/create` and assign it the role we created above.

4. Enable module

Visit `/admin/modules` and enable the Basic Auth module.

5. Connect Drupal

To connect the Next.js project to Drupal, we use environment variables.

Fill in the following variables in your `.env.local` file.

.env.local

BASIC_AUTH_USERNAME=
BASIC_AUTH_PASSWORD=

This is the username and password of the user we created above.

6. Update DrupalClient

Update the `DrupalClient` to use the Basic authentication header.

lib/drupal.ts

import { Experiment_DrupalClient as DrupalClient } from "next-drupal"
export const drupal = new DrupalClient(
process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
{
auth: () =>
"Basic " +
Buffer.from(
`${process.env.BASIC_AUTH_USERNAME}:${process.env.BASIC_AUTH_PASSWORD}`
).toString("base64"),
}
)