Authentication

Using a custom authentication with DrupalClient.


⚠️

The `DrupalClient` is available in `next-drupal ^1.3.0` as experimental.

The `DrupalClient` works with several auth types. You can use Bearer (`client_credentials`), Basic or bring your own authorization headers.


Bearer

To configure authentication, provide a `client_id` and `client_secret` as options.

⚠️

You need to enable the `simple_oauth` module on Drupal.

lib/drupal.ts

import { Experiment_DrupalClient as DrupalClient } from "next-drupal"
export const drupal = new Experiment_DrupalClient(
process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
{
auth: {
clientId: process.env.DRUPAL_CLIENT_ID,
clientSecret: process.env.DRUPAL_CLIENT_SECRET,
},
}
)

`DrupalClient` will fetch Bearer token and handle expiration for you.

If you need to customize the OAuth url you can use the `url` option.

lib/drupal.ts

import { Experiment_DrupalClient as DrupalClient } from "next-drupal"
export const drupal = new DrupalClient(
process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
{
auth: {
clientId: process.env.DRUPAL_CLIENT_ID,
clientSecret: process.env.DRUPAL_CLIENT_SECRET,
url: `/oauth2/token`,
},
}
)

Basic

You can also use the basic authorization header as follows:

⚠️

You need to enable the `basic_auth` module on Drupal.

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"),
}
)

Callback

You can also provide a custom callback for authentication. The callback must return an `Authorization` compatible 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: () => {
// Do something and return an Authorization header.
},
}
)