Image

Use Fetch and OAuth to Make Authenticated Requests for Drupal 8, 9, 10, and 11

To perform POST, PUT, and DELETE operations to Drupal's JSON:API from a decoupled React app, you need an OAuth 2 access token. Modern best practice for browser apps is to use the authorization code grant with PKCE (Proof Key for Code Exchange). You'll redirect the user to Drupal to sign in, receive an authorization code at your redirect URI, exchange it for tokens, attach the access token to API requests, and refresh it when it expires.

In this tutorial we'll:

  • Add lightweight OAuth helper functions that implement authorization code + PKCE and refresh.
  • Create a tiny UI to trigger the log in flow and handle the auth callback.
  • Wrap fetch() to automatically include Authorization: Bearer ... and handle refresh.
  • And finally, update our existing components to use that wrapper.

By the end of this tutorial, you should be able to make authenticated JSON:API requests from React using fetch with a modern OAuth flow.

Decoupled Headless Drupal